<template>
  <section
    class="h-full shadow-[1px_0px_4px_rgba(0,0,0,0.25)] bg-neutral-100 absolute inset-y-0 left-0 z-10"
    :class="
      !notShowActList ? (isShowBarOpen ? 'w-full' : 'w-[312px]') : 'w-[312px]'
    "
  >
    <!-- header -->
    <div class="bg-neutral-200 px-4">
      <p class="h2">{{ i18next.t("Conformance.RuleSettings") }}</p>
    </div>
    <!-- contanier -->
    <div class="p-4 h-[calc(100%_-_40px)] flex">
      <div class="h-full flex flex-col justify-between w-[280px]">
        <!-- select -->
        <div class="h-full overflow-y-auto scrollbar w-[280px]">
          <!-- radio group -->
          <ConformanceRadioGroup></ConformanceRadioGroup>
          <!-- show and hidden button -->
          <div
            v-if="!notShowActList"
            class="flex items-center justify-between mr-1"
            :class="isShowBarOpen ? 'text-primary' : ''"
          >
            <p class="h2">{{ i18next.t("Conformance.ActivitySelector") }}</p>
            <span
              id="cyp-showbaricon"
              class="material-symbols-outlined cursor-pointer duration-300 hover:bg-primary/50 hover:rounded"
              @click="isShowBarOpen = !isShowBarOpen"
            >
              {{
                isShowBarOpen
                  ? "keyboard_double_arrow_right"
                  : "keyboard_double_arrow_left"
              }}
            </span>
          </div>
          <!-- select result -->
          <ConformanceSelectResult
            :isSubmit="isAlreadySubmit"
            :isSubmitTask="isSubmitTask"
            :isSubmitStartAndEnd="isSubmitStartAndEnd"
            :isSubmitCfmSeqDirectly="isSubmitCfmSeqDirectly"
            :isSubmitCfmSeqEventually="isSubmitCfmSeqEventually"
            :isSubmitDurationData="isSubmitDurationData"
            :isSubmitCfmPtEteStart="isSubmitCfmPtEteStart"
            :isSubmitCfmPtEteEnd="isSubmitCfmPtEteEnd"
            :isSubmitCfmPtEteSE="isSubmitCfmPtEteSE"
            :isSubmitCfmPtPStart="isSubmitCfmPtPStart"
            :isSubmitCfmPtPEnd="isSubmitCfmPtPEnd"
            :isSubmitCfmPtPSE="isSubmitCfmPtPSE"
            :isSubmitCfmWtEteStart="isSubmitCfmWtEteStart"
            :isSubmitCfmWtEteEnd="isSubmitCfmWtEteEnd"
            :isSubmitCfmWtEteSE="isSubmitCfmWtEteSE"
            :isSubmitCfmWtPStart="isSubmitCfmWtPStart"
            :isSubmitCfmWtPEnd="isSubmitCfmWtPEnd"
            :isSubmitCfmWtPSE="isSubmitCfmWtPSE"
            :isSubmitCfmCtEteStart="isSubmitCfmCtEteStart"
            :isSubmitCfmCtEteEnd="isSubmitCfmCtEteEnd"
            :isSubmitCfmCtEteSE="isSubmitCfmCtEteSE"
          ></ConformanceSelectResult>
          <!-- Time Range -->
          <ConformanceTimeRange
            @min-total-seconds="minTotalSeconds"
            @max-total-seconds="maxTotalSeconds"
            :isSubmitDurationTime="isSubmitDurationTime"
            :isSubmitTimeCfmPtEteAll="isSubmitTimeCfmPtEteAll"
            :isSubmitTimeCfmPtEteStart="isSubmitTimeCfmPtEteStart"
            :isSubmitTimeCfmPtEteEnd="isSubmitTimeCfmPtEteEnd"
            :isSubmitTimeCfmPtEteSE="isSubmitTimeCfmPtEteSE"
            :isSubmitTimeCfmPtPStart="isSubmitTimeCfmPtPStart"
            :isSubmitTimeCfmPtPEnd="isSubmitTimeCfmPtPEnd"
            :isSubmitTimeCfmPtPSE="isSubmitTimeCfmPtPSE"
            :isSubmitTimeCfmWtEteAll="isSubmitTimeCfmWtEteAll"
            :isSubmitTimeCfmWtEteStart="isSubmitTimeCfmWtEteStart"
            :isSubmitTimeCfmWtEteEnd="isSubmitTimeCfmWtEteEnd"
            :isSubmitTimeCfmWtEteSE="isSubmitTimeCfmWtEteSE"
            :isSubmitTimeCfmWtPStart="isSubmitTimeCfmWtPStart"
            :isSubmitTimeCfmWtPEnd="isSubmitTimeCfmWtPEnd"
            :isSubmitTimeCfmWtPSE="isSubmitTimeCfmWtPSE"
            :isSubmitTimeCfmCtEteAll="isSubmitTimeCfmCtEteAll"
            :isSubmitTimeCfmCtEteStart="isSubmitTimeCfmCtEteStart"
            :isSubmitTimeCfmCtEteEnd="isSubmitTimeCfmCtEteEnd"
            :isSubmitTimeCfmCtEteSE="isSubmitTimeCfmCtEteSE"
          ></ConformanceTimeRange>
        </div>
        <!-- buttons -->
        <div
          class="space-x-4 p-4 flex justify-center items-content border-t border-neutral-300"
        >
          <button type="button" class="btn btn-sm btn-neutral" @click="reset">
            {{ i18next.t("Global.Clear") }}
          </button>
          <button
            id="btn_apply_conformance"
            type="button"
            class="btn btn-sm"
            @click="submitConformance"
            :class="isApplyBtnDisabled ? 'btn-disable' : 'btn-neutral'"
            :disabled="isApplyBtnDisabled"
          >
            {{ i18next.t("Global.Apply") }}
          </button>
        </div>
      </div>
      <!-- show bar -->
      <ConformanceShowBar
        v-if="!notShowActList"
        :class="isShowBarOpen ? '' : 'hidden'"
        :isSubmit="isAlreadySubmit"
        :isSubmitTask="isSubmitTask"
        :isSubmitStartAndEnd="isSubmitStartAndEnd"
        :isSubmitCfmSeqDirectly="isSubmitCfmSeqDirectly"
        :isSubmitCfmSeqEventually="isSubmitCfmSeqEventually"
        :isSubmitDurationData="isSubmitDurationData"
        :isSubmitCfmPtEteStart="isSubmitCfmPtEteStart"
        :isSubmitCfmPtEteEnd="isSubmitCfmPtEteEnd"
        :isSubmitCfmPtEteSE="isSubmitCfmPtEteSE"
        :isSubmitCfmPtPStart="isSubmitCfmPtPStart"
        :isSubmitCfmPtPEnd="isSubmitCfmPtPEnd"
        :isSubmitCfmPtPSE="isSubmitCfmPtPSE"
        :isSubmitCfmWtEteStart="isSubmitCfmWtEteStart"
        :isSubmitCfmWtEteEnd="isSubmitCfmWtEteEnd"
        :isSubmitCfmWtEteSE="isSubmitCfmWtEteSE"
        :isSubmitCfmWtPStart="isSubmitCfmWtPStart"
        :isSubmitCfmWtPEnd="isSubmitCfmWtPEnd"
        :isSubmitCfmWtPSE="isSubmitCfmWtPSE"
        :isSubmitCfmCtEteStart="isSubmitCfmCtEteStart"
        :isSubmitCfmCtEteEnd="isSubmitCfmCtEteEnd"
        :isSubmitCfmCtEteSE="isSubmitCfmCtEteSE"
        :isSubmitShowDataSeq="isSubmitShowDataSeq"
        :isSubmitShowDataPtEte="isSubmitShowDataPtEte"
        :isSubmitShowDataPtP="isSubmitShowDataPtP"
        :isSubmitShowDataWtEte="isSubmitShowDataWtEte"
        :isSubmitShowDataWtP="isSubmitShowDataWtP"
        :isSubmitShowDataCt="isSubmitShowDataCt"
      ></ConformanceShowBar>
    </div>
  </section>
</template>

<script setup>
// The Lucia project.
// Copyright 2023-2026 DSP, inc.  All rights reserved.
// Authors:
//   chiayin.kuo@dsp.im (chiayin), 2023/1/31
//   imacat.yang@dsp.im (imacat), 2023/9/23
//   cindy.chang@dsp.im (Cindy Chang), 2024/5/30
/**
 * @module components/Discover/Conformance/ConformanceSidebar
 *   Main sidebar for conformance checking with rule
 *   configuration, activity selection, and submit/reset
 *   actions.
 */

import { ref, computed, watch, onBeforeUnmount } from "vue";
import { storeToRefs } from "pinia";
import { useToast } from "vue-toast-notification";
import i18next from "@/i18n/i18n";
import emitter from "@/utils/emitter";
import { useConformanceInputStore } from "@/stores/conformanceInput";
import { useLoadingStore } from "@/stores/loading";
import { useConformanceStore } from "@/stores/conformance";
import ConformanceRadioGroup from "./ConformanceSidebar/ConformanceRadioGroup.vue";
import ConformanceShowBar from "./ConformanceSidebar/ConformanceShowBar.vue";
import ConformanceTimeRange from "./ConformanceSidebar/ConformanceTimeRange.vue";
import ConformanceSelectResult from "./ConformanceSidebar/ConformanceSelectResult.vue";
import { cloneDeep } from "lodash-es";

const $toast = useToast();

const loadingStore = useLoadingStore();
const conformanceStore = useConformanceStore();
const conformanceInputStore = useConformanceInputStore();
const { isLoading } = storeToRefs(loadingStore);
const {
  selectedRuleType,
  selectedActivitySequence,
  selectedMode,
  selectedProcessScope,
  selectedActSeqMore,
  selectedActSeqFromTo,
  conformanceAllTasks,
  cfmPtEteWhole,
  cfmPtEteStart: cfmPtEteStartStore,
  cfmPtEteEnd: cfmPtEteEndStore,
  cfmPtEteSE,
  cfmPtPStart: cfmPtPStartStore,
  cfmPtPEnd: cfmPtPEndStore,
  cfmPtPSE,
  cfmWtEteWhole,
  cfmWtEteStart: cfmWtEteStartStore,
  cfmWtEteEnd: cfmWtEteEndStore,
  cfmWtEteSE,
  cfmWtPStart: cfmWtPStartStore,
  cfmWtPEnd: cfmWtPEndStore,
  cfmWtPSE,
  cfmCtEteWhole,
  cfmCtEteStart: cfmCtEteStartStore,
  cfmCtEteEnd: cfmCtEteEndStore,
  cfmCtEteSE,
  isStartSelected,
  isEndSelected,
  conformanceRuleData,
  conformanceLogCreateCheckId,
  conformanceFilterCreateCheckId,
  conformanceTempReportData,
} = storeToRefs(conformanceStore);
const { activityRadioData } = storeToRefs(conformanceInputStore);

// data
const isShowBarOpen = ref(false);
const selectConformanceTask = ref(null);
const selectConformanceStartAndEnd = ref(null); // Activity sequence
const selectCfmSeqStart = ref(null);
const selectCfmSeqEnd = ref(null);
const selectCfmSeqDirectly = ref([]);
const selectCfmSeqEventually = ref([]);
const selectDurationData = ref(null); // Activity duration
const selectDurationTime = ref({ min: 0, max: 0 });
const selectTimeRangeMin = ref(0); // Time Range min
const selectTimeRangeMax = ref(0); // Time Range max
const selectCfmPtEteStart = ref(null); // Processing time
const selectCfmPtEteEnd = ref(null);
const selectCfmPtEteSEStart = ref(null);
const selectCfmPtEteSEEnd = ref(null);
const selectCfmPtPStart = ref(null);
const selectCfmPtPEnd = ref(null);
const selectCfmPtPSEStart = ref(null);
const selectCfmPtPSEEnd = ref(null);
const selectCfmWtEteStart = ref(null); // Waiting time
const selectCfmWtEteEnd = ref(null);
const selectCfmWtEteSEStart = ref(null);
const selectCfmWtEteSEEnd = ref(null);
const selectCfmWtPStart = ref(null);
const selectCfmWtPEnd = ref(null);
const selectCfmWtPSEStart = ref(null);
const selectCfmWtPSEEnd = ref(null);
const selectCfmCtEteStart = ref(null); // Cycle time
const selectCfmCtEteEnd = ref(null);
const selectCfmCtEteSEStart = ref(null);
const selectCfmCtEteSEEnd = ref(null);
const isAlreadySubmit = ref(false);
const isSubmittedData = ref(null); // Data after Apply, unchanged unless rules are modified
const isSubmitTask = ref(null);
const isSubmitStartAndEnd = ref(null); // Activity sequence
const isSubmitCfmSeqDirectly = ref([]);
const isSubmitCfmSeqEventually = ref([]);
const isSubmitDurationData = ref(null); // Activity duration
const isSubmitCfmPtEteStart = ref(null); // Processing time
const isSubmitCfmPtEteEnd = ref(null);
const isSubmitCfmPtEteSE = ref(null);
const isSubmitCfmPtPStart = ref(null);
const isSubmitCfmPtPEnd = ref(null);
const isSubmitCfmPtPSE = ref(null);
const isSubmitCfmWtEteStart = ref(null); // Waiting time
const isSubmitCfmWtEteEnd = ref(null);
const isSubmitCfmWtEteSE = ref(null);
const isSubmitCfmWtPStart = ref(null);
const isSubmitCfmWtPEnd = ref(null);
const isSubmitCfmWtPSE = ref(null);
const isSubmitCfmCtEteStart = ref(null); // Cycle time
const isSubmitCfmCtEteEnd = ref(null);
const isSubmitCfmCtEteSE = ref(null);
const isSubmitDurationTime = ref({ base: {}, rule: {} }); // Activity duration Time Range
const isSubmitTimeCfmPtEteAll = ref({ base: {}, rule: {} }); // Processing time Time Range
const isSubmitTimeCfmPtEteStart = ref({ base: {}, rule: {} });
const isSubmitTimeCfmPtEteEnd = ref({ base: {}, rule: {} });
const isSubmitTimeCfmPtEteSE = ref({ base: {}, rule: {} });
const isSubmitTimeCfmPtPStart = ref({ base: {}, rule: {} });
const isSubmitTimeCfmPtPEnd = ref({ base: {}, rule: {} });
const isSubmitTimeCfmPtPSE = ref({ base: {}, rule: {} });
const isSubmitTimeCfmWtEteAll = ref({ base: {}, rule: {} }); // Waiting time Time Range
const isSubmitTimeCfmWtEteStart = ref({ base: {}, rule: {} });
const isSubmitTimeCfmWtEteEnd = ref({ base: {}, rule: {} });
const isSubmitTimeCfmWtEteSE = ref({ base: {}, rule: {} });
const isSubmitTimeCfmWtPStart = ref({ base: {}, rule: {} });
const isSubmitTimeCfmWtPEnd = ref({ base: {}, rule: {} });
const isSubmitTimeCfmWtPSE = ref({ base: {}, rule: {} });
const isSubmitTimeCfmCtEteAll = ref({ base: {}, rule: {} }); // Cycle time Time Range
const isSubmitTimeCfmCtEteStart = ref({ base: {}, rule: {} });
const isSubmitTimeCfmCtEteEnd = ref({ base: {}, rule: {} });
const isSubmitTimeCfmCtEteSE = ref({ base: {}, rule: {} });
const isSubmitShowDataSeq = ref({
  task: null,
  taskStart: null,
  taskEnd: null,
  isStartSelected: null,
  isEndSelected: null,
}); // Linked data for Start & End
const isSubmitShowDataPtEte = ref({
  task: null,
  taskStart: null,
  taskEnd: null,
  isStartSelected: null,
  isEndSelected: null,
});
const isSubmitShowDataPtP = ref({
  task: null,
  taskStart: null,
  taskEnd: null,
  isStartSelected: null,
  isEndSelected: null,
});
const isSubmitShowDataWtEte = ref({
  task: null,
  taskStart: null,
  taskEnd: null,
  isStartSelected: null,
  isEndSelected: null,
});
const isSubmitShowDataWtP = ref({
  task: null,
  taskStart: null,
  taskEnd: null,
  isStartSelected: null,
  isEndSelected: null,
});
const isSubmitShowDataCt = ref({
  task: null,
  taskStart: null,
  taskEnd: null,
  isStartSelected: null,
  isEndSelected: null,
});

// Map for dynamic field access in handleSimpleSelection
const selectFieldRefs = {
  selectCfmPtEteStart,
  selectCfmPtEteEnd,
  selectCfmPtPStart,
  selectCfmPtPEnd,
  selectCfmWtEteStart,
  selectCfmWtEteEnd,
  selectCfmWtPStart,
  selectCfmWtPEnd,
  selectCfmCtEteStart,
  selectCfmCtEteEnd,
};

// computed
const notShowActList = computed(() => {
  return (
    (selectedRuleType.value === "Activity sequence" &&
      selectedActivitySequence.value === "Sequence" &&
      (selectedMode.value === "Short loop(s)" ||
        selectedMode.value === "Self loop(s)")) ||
    (selectedRuleType.value === "Processing time" &&
      selectedProcessScope.value === "End to end" &&
      selectedActSeqMore.value === "All") ||
    (selectedRuleType.value === "Waiting time" &&
      selectedProcessScope.value === "End to end" &&
      selectedActSeqMore.value === "All") ||
    (selectedRuleType.value === "Cycle time" &&
      selectedProcessScope.value === "End to end" &&
      selectedActSeqMore.value === "All")
  );
});

/**
 * Apply button is disabled or not
 */
const isApplyBtnDisabled = computed(() => {
  let disabledBool = true;

  switch (selectedRuleType.value) {
    case "Have activity":
      disabledBool = checkHaveActivity();
      break;
    case "Activity sequence":
      disabledBool = checkActivitySequence();
      break;
    case "Activity duration":
      disabledBool = checkActivityDuration();
      break;
    case "Processing time":
      disabledBool = checkProcessingTime();
      break;
    case "Waiting time":
      disabledBool = checkWaitingTime();
      break;
    case "Cycle time":
      disabledBool = checkCycleTime();
      break;
    default:
      break;
  }

  return disabledBool;
});

// When opening a rule file, display the saved options, rules, and time
let reportWatchTimerId = null;
watch(conformanceTempReportData, (newValue) => {
  if (reportWatchTimerId) clearTimeout(reportWatchTimerId);
  reportWatchTimerId = setTimeout(() => {
    if (newValue !== null) {
      const rule = newValue.rule;

      isSubmittedData.value = newValue.rule;
      isAlreadySubmit.value = true;
      switch (rule.type) {
        case "contains-tasks": // Behavior when Rule Type is Have activity
          selectedRuleType.value = "Have activity";
          isSubmitTask.value = rule.tasks;
          break;
        case "start-end": // Behavior when Rule Type is Activity sequence
          selectedRuleType.value = "Activity sequence";
          selectedActivitySequence.value = "Start & End";
          isSubmitStartAndEnd.value = [
            { category: "Start", task: rule.starts_with },
            { category: "End", task: rule.ends_with },
          ];
          break;
        case "directly-follows": // Behavior when Activity Sequence is Sequence
          selectedRuleType.value = "Activity sequence";
          selectedActivitySequence.value = "Sequence";
          selectedMode.value = "Directly follows";
          isSubmitCfmSeqDirectly.value = rule.task_seq;
          break;
        case "eventually-follows":
          selectedRuleType.value = "Activity sequence";
          selectedActivitySequence.value = "Sequence";
          selectedMode.value = "Eventually follows";
          isSubmitCfmSeqEventually.value = rule.task_seq;
          break;
        case "short-loops":
          selectedRuleType.value = "Activity sequence";
          selectedActivitySequence.value = "Sequence";
          selectedMode.value = "Short loop(s)";
          break;
        case "self-loops":
          selectedRuleType.value = "Activity sequence";
          selectedActivitySequence.value = "Sequence";
          selectedMode.value = "Self loop(s)";
          break;
        case "task-duration": // Behavior when Rule Type is Activity duration
          selectedRuleType.value = "Activity duration";
          selectDurationData.value = [rule.task];
          isSubmitDurationData.value = [rule.task];
          isSubmitDurationTime.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "processing-time-end-to-end-whole": // Behavior when Rule Type is Processing time
          selectedRuleType.value = "Processing time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "All";
          isSubmitTimeCfmPtEteAll.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "processing-time-end-to-end-starts-with":
          selectedRuleType.value = "Processing time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "Start";
          selectCfmPtEteStart.value = rule.task;
          isSubmitCfmPtEteStart.value = [
            { category: "Start", task: rule.task },
          ];
          isSubmitTimeCfmPtEteStart.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "processing-time-end-to-end-ends-with":
          selectedRuleType.value = "Processing time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "End";
          selectCfmPtEteEnd.value = rule.task;
          isSubmitCfmPtEteEnd.value = [{ category: "End", task: rule.task }];
          isSubmitTimeCfmPtEteEnd.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "processing-time-end-to-end-start-end":
          selectedRuleType.value = "Processing time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "Start & End";
          isSubmitCfmPtEteSE.value = [
            { category: "Start", task: rule.start },
            { category: "End", task: rule.end },
          ];
          isSubmitTimeCfmPtEteSE.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          isSubmitShowDataPtEte.value = setSubmitShowDataByStartEnd(
            rule.start,
            rule.end,
          );
          break;
        case "processing-time-partial-starts-with":
          selectedRuleType.value = "Processing time";
          selectedProcessScope.value = "Partial";
          selectedActSeqFromTo.value = "From";
          selectCfmPtPStart.value = rule.task;
          isSubmitCfmPtPStart.value = [{ category: "From", task: rule.task }];
          isSubmitTimeCfmPtPStart.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "processing-time-partial-ends-with":
          selectedRuleType.value = "Processing time";
          selectedProcessScope.value = "Partial";
          selectedActSeqFromTo.value = "To";
          selectCfmPtPEnd.value = rule.task;
          isSubmitCfmPtPEnd.value = [{ category: "To", task: rule.task }];
          isSubmitTimeCfmPtPEnd.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "processing-time-partial-start-end":
          selectedRuleType.value = "Processing time";
          selectedProcessScope.value = "Partial";
          selectedActSeqFromTo.value = "From & To";
          isSubmitCfmPtPSE.value = [
            { category: "From", task: rule.start },
            { category: "To", task: rule.end },
          ];
          isSubmitTimeCfmPtPSE.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          isSubmitShowDataPtP.value = setSubmitShowDataByStartEnd(
            rule.start,
            rule.end,
          );
          break;
        case "waiting-time-end-to-end-whole": // Behavior when Rule Type is Waiting time
          selectedRuleType.value = "Waiting time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "All";
          isSubmitTimeCfmWtEteAll.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "waiting-time-end-to-end-starts-with":
          selectedRuleType.value = "Waiting time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "Start";
          selectCfmWtEteStart.value = rule.task;
          isSubmitCfmWtEteStart.value = [
            { category: "Start", task: rule.task },
          ];
          isSubmitTimeCfmWtEteStart.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "waiting-time-end-to-end-ends-with":
          selectedRuleType.value = "Waiting time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "End";
          selectCfmWtEteEnd.value = rule.task;
          isSubmitCfmWtEteEnd.value = [{ category: "End", task: rule.task }];
          isSubmitTimeCfmWtEteEnd.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "waiting-time-end-to-end-start-end":
          selectedRuleType.value = "Waiting time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "Start & End";
          isSubmitCfmWtEteSE.value = [
            { category: "Start", task: rule.start },
            { category: "End", task: rule.end },
          ];
          isSubmitTimeCfmWtEteSE.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          isSubmitShowDataWtEte.value = setSubmitShowDataByStartEnd(
            rule.start,
            rule.end,
          );
          break;
        case "waiting-time-partial-starts-with":
          selectedRuleType.value = "Waiting time";
          selectedProcessScope.value = "Partial";
          selectedActSeqFromTo.value = "From";
          selectCfmWtPStart.value = rule.task;
          isSubmitCfmWtPStart.value = [{ category: "From", task: rule.task }];
          isSubmitTimeCfmWtPStart.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "waiting-time-partial-ends-with":
          selectedRuleType.value = "Waiting time";
          selectedProcessScope.value = "Partial";
          selectedActSeqFromTo.value = "To";
          selectCfmWtPEnd.value = rule.task;
          isSubmitCfmWtPEnd.value = [{ category: "To", task: rule.task }];
          isSubmitTimeCfmWtPEnd.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "waiting-time-partial-start-end":
          selectedRuleType.value = "Waiting time";
          selectedProcessScope.value = "Partial";
          selectedActSeqFromTo.value = "From & To";
          isSubmitCfmWtPSE.value = [
            { category: "From", task: rule.start },
            { category: "To", task: rule.end },
          ];
          isSubmitTimeCfmWtPSE.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          isSubmitShowDataWtP.value = setSubmitShowDataByStartEnd(
            rule.start,
            rule.end,
          );
          break;
        case "cycle-time-end-to-end-whole": // Behavior when Rule Type is Cycle time
          selectedRuleType.value = "Cycle time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "All";
          isSubmitTimeCfmCtEteAll.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "cycle-time-end-to-end-starts-with":
          selectedRuleType.value = "Cycle time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "Start";
          selectCfmCtEteStart.value = rule.task;
          isSubmitCfmCtEteStart.value = [
            { category: "Start", task: rule.task },
          ];
          isSubmitTimeCfmCtEteStart.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "cycle-time-end-to-end-ends-with":
          selectedRuleType.value = "Cycle time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "End";
          selectCfmCtEteEnd.value = rule.task;
          isSubmitCfmCtEteEnd.value = [{ category: "End", task: rule.task }];
          isSubmitTimeCfmCtEteEnd.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          break;
        case "cycle-time-end-to-end-start-end":
          selectedRuleType.value = "Cycle time";
          selectedProcessScope.value = "End to end";
          selectedActSeqMore.value = "Start & End";
          isSubmitCfmCtEteSE.value = [
            { category: "Start", task: rule.start },
            { category: "End", task: rule.end },
          ];
          isSubmitTimeCfmCtEteSE.value = {
            base: newValue.charts.time.x_axis,
            rule: { min: rule.min, max: rule.max },
          };
          isSubmitShowDataCt.value = setSubmitShowDataByStartEnd(
            rule.start,
            rule.end,
          );
          break;
      }
    }
  }, 300);
});

// methods
/**
 * get min total seconds
 * @param {number} e - The minimum total seconds.
 */
function minTotalSeconds(e) {
  selectTimeRangeMin.value = e;
}
/**
 * get min total seconds
 * @param {number} e - The maximum total seconds.
 */
function maxTotalSeconds(e) {
  selectTimeRangeMax.value = e;
}
/**
 * select Time Reset
 */
function selectTimeReset() {
  // Does not include selectDurationTime
  selectConformanceTask.value = null; // Have activity
  selectConformanceStartAndEnd.value = null; // Activity Sequence
  selectCfmSeqStart.value = null;
  selectCfmSeqEnd.value = null;
  selectCfmSeqDirectly.value = [];
  selectCfmSeqEventually.value = [];
  selectDurationData.value = null; // Activity durations
  selectTimeRangeMin.value = 0; // Time Range min
  selectTimeRangeMax.value = 0; // Time Range max
  selectCfmPtEteStart.value = null; // Processing time
  selectCfmPtEteEnd.value = null;
  selectCfmPtEteSEStart.value = null;
  selectCfmPtEteSEEnd.value = null;
  selectCfmPtPStart.value = null;
  selectCfmPtPEnd.value = null;
  selectCfmPtPSEStart.value = null;
  selectCfmPtPSEEnd.value = null;
  selectCfmWtEteStart.value = null; // Waiting time
  selectCfmWtEteEnd.value = null;
  selectCfmWtEteSEStart.value = null;
  selectCfmWtEteSEEnd.value = null;
  selectCfmWtPStart.value = null;
  selectCfmWtPEnd.value = null;
  selectCfmWtPSEStart.value = null;
  selectCfmWtPSEEnd.value = null;
  selectCfmCtEteStart.value = null; // Cycle time
  selectCfmCtEteEnd.value = null;
  selectCfmCtEteSEStart.value = null;
  selectCfmCtEteSEEnd.value = null;
}
/**
 * is submit select Reset
 */
function isSubmitReset() {
  // Does not include isSubmittedData
  isSubmitTask.value = null;
  isSubmitStartAndEnd.value = null;
  isSubmitCfmSeqDirectly.value = [];
  isSubmitCfmSeqEventually.value = [];
  isSubmitDurationData.value = null;
  isSubmitDurationTime.value = { base: {}, rule: {} };
  isSubmitCfmPtEteStart.value = null; // Processing time
  isSubmitCfmPtEteEnd.value = null;
  isSubmitCfmPtEteSE.value = null;
  isSubmitCfmPtPStart.value = null;
  isSubmitCfmPtPEnd.value = null;
  isSubmitCfmPtPSE.value = null;
  isSubmitCfmWtEteStart.value = null; // Waiting time
  isSubmitCfmWtEteEnd.value = null;
  isSubmitCfmWtEteSE.value = null;
  isSubmitCfmWtPStart.value = null;
  isSubmitCfmWtPEnd.value = null;
  isSubmitCfmWtPSE.value = null;
  isSubmitCfmCtEteStart.value = null; // Cycle time
  isSubmitCfmCtEteEnd.value = null;
  isSubmitCfmCtEteSE.value = null;
  isSubmitTimeCfmPtEteAll.value = { base: {}, rule: {} }; // Processing time Time Range
  isSubmitTimeCfmPtEteStart.value = { base: {}, rule: {} };
  isSubmitTimeCfmPtEteEnd.value = { base: {}, rule: {} };
  isSubmitTimeCfmPtEteSE.value = { base: {}, rule: {} };
  isSubmitTimeCfmPtPStart.value = { base: {}, rule: {} };
  isSubmitTimeCfmPtPEnd.value = { base: {}, rule: {} };
  isSubmitTimeCfmPtPSE.value = { base: {}, rule: {} };
  isSubmitTimeCfmWtEteAll.value = { base: {}, rule: {} }; // Waiting time Time Range
  isSubmitTimeCfmWtEteStart.value = { base: {}, rule: {} };
  isSubmitTimeCfmWtEteEnd.value = { base: {}, rule: {} };
  isSubmitTimeCfmWtEteSE.value = { base: {}, rule: {} };
  isSubmitTimeCfmWtPStart.value = { base: {}, rule: {} };
  isSubmitTimeCfmWtPEnd.value = { base: {}, rule: {} };
  isSubmitTimeCfmWtPSE.value = { base: {}, rule: {} };
  isSubmitTimeCfmCtEteAll.value = { base: {}, rule: {} }; // Cycle time Time Range
  isSubmitTimeCfmCtEteStart.value = { base: {}, rule: {} };
  isSubmitTimeCfmCtEteEnd.value = { base: {}, rule: {} };
  isSubmitTimeCfmCtEteSE.value = { base: {}, rule: {} };
}
/**
 * Clears all form selections and resets the sidebar state.
 */
function reset() {
  // Results page cover plate (mask enabled)
  emitter.emit("coverPlate", true);
  // is submit select Reset
  isSubmitReset();
  isSubmittedData.value = null; // Data after Apply, unchanged unless rules are modified
  isAlreadySubmit.value = false;

  // Reset other child components
  selectTimeReset();
  emitter.emit("reset", null);
  // Reset success message
  $toast.success(i18next.t("Conformance.RuleCleared"));
  isShowBarOpen.value = true;
}
/**
 * Sets linked Start & End data for activity selection.
 * @param {string} start task
 * @param {string} end task
 * @returns {object}
 */
function setSubmitShowDataByStartEnd(start, end) {
  if (isStartSelected.value) {
    return {
      task: start,
      taskStart: start,
      taskEnd: end,
      isStartSelected: true,
      isEndSelected: false,
    };
  } else {
    return {
      task: end,
      taskStart: start,
      taskEnd: end,
      isStartSelected: false,
      isEndSelected: true,
    };
  }
}
/**
 * Submits the selected options via the Apply button and retrieves the Check ID.
 */
async function submitConformance() {
  let dataToSave;

  selectDurationTime.value = {
    min: selectTimeRangeMin.value,
    max: selectTimeRangeMax.value,
  };
  emitter.emit("timeRangeMaxMin", selectDurationTime.value);

  switch (selectedRuleType.value) {
    case "Have activity": // Behavior when Rule Type is Have activity
      dataToSave = getHaveActivityData();
      break;
    case "Activity sequence": // Behavior when Rule Type is Activity sequence
      dataToSave = getActivitySequenceData();
      break;
    case "Activity duration": // Behavior when Rule Type is Activity duration
      dataToSave = getActivityDurationData();
      break;
    case "Processing time": // Behavior when Rule Type is Processing time
      dataToSave = getProcessingTimeData();
      break;
    case "Waiting time": // Behavior when Rule Type is Waiting time
      dataToSave = getWaitingTimeData();
      break;
    case "Cycle time": // Behavior when Rule Type is Cycle time
      dataToSave = getCycleTimeData();
      break;
  }

  if (dataToSave.min > dataToSave.max) {
    return $toast.error(i18next.t("Conformance.PleaseCheckTimeRange"));
  }
  if (JSON.stringify(dataToSave) === JSON.stringify(isSubmittedData.value)) {
    return $toast.error(i18next.t("Conformance.PleaseSetNewRule"));
  }

  isLoading.value = true;
  isAlreadySubmit.value = true;
  isSubmittedData.value = dataToSave; // Data after Apply, unchanged unless rules are modified
  conformanceRuleData.value = dataToSave; // Data for saving to file
  await conformanceStore.addConformanceCheckId(dataToSave);
  await conformanceStore.getConformanceReport();
  isShowBarOpen.value = false;
  isLoading.value = false;
  // Results page Cover Plate
  emitter.emit("coverPlate", false);
  $toast.success(i18next.t("Conformance.RuleApplied"));
}

/** Builds the data payload for the 'Have activity' rule.
 * @returns {object} The rule data to submit.
 */
function getHaveActivityData() {
  const dataToSave = {
    type: "contains-tasks",
    tasks: selectConformanceTask.value,
  };
  isSubmitReset();
  isSubmitTask.value = selectConformanceTask.value;
  return dataToSave;
}

/** Builds the data payload for the 'Activity sequence' rule.
 * @returns {object} The rule data to submit.
 */
function getActivitySequenceData() {
  let dataToSave;
  switch (selectedActivitySequence.value) {
    case "Start & End":
      dataToSave = {
        type: "start-end",
        starts_with: selectCfmSeqStart.value,
        ends_with: selectCfmSeqEnd.value,
      };
      isSubmitReset();
      isSubmitStartAndEnd.value = [
        { category: "Start", task: selectCfmSeqStart.value },
        { category: "End", task: selectCfmSeqEnd.value },
      ];
      isSubmitShowDataSeq.value = setSubmitShowDataByStartEnd(
        selectCfmSeqStart.value,
        selectCfmSeqEnd.value,
      );
      break;
    case "Sequence":
      dataToSave = getSequenceData();
      break;
  }
  return dataToSave;
}

/** Builds the data payload for sequence sub-rules.
 * @returns {object} The rule data to submit.
 */
function getSequenceData() {
  let dataToSave;
  switch (selectedMode.value) {
    case "Directly follows":
      dataToSave = {
        type: "directly-follows",
        task_seq: selectCfmSeqDirectly.value,
      };
      isSubmitReset();
      isSubmitCfmSeqDirectly.value = cloneDeep(selectCfmSeqDirectly.value);
      break;
    case "Eventually follows":
      dataToSave = {
        type: "eventually-follows",
        task_seq: selectCfmSeqEventually.value,
      };
      isSubmitReset();
      isSubmitCfmSeqEventually.value = cloneDeep(selectCfmSeqEventually.value);
      break;
    case "Short loop(s)":
      dataToSave = { type: "short-loops" };
      break;
    case "Self loop(s)":
      dataToSave = { type: "self-loops" };
      break;
  }
  return dataToSave;
}

/** Builds the data payload for the 'Activity duration' rule.
 * @returns {object} The rule data to submit.
 */
function getActivityDurationData() {
  const dataToSave = {
    type: "task-duration",
    task: selectDurationData.value[0],
    min: selectDurationTime.value.min,
    max: selectDurationTime.value.max,
  };
  isSubmitReset();
  isSubmitDurationData.value = selectDurationData.value;
  isSubmitDurationTime.value.rule = selectDurationTime.value;
  return dataToSave;
}

/** Builds the data payload for the 'Processing time' rule.
 * @returns {object} The rule data to submit.
 */
function getProcessingTimeData() {
  let dataToSave;
  switch (selectedProcessScope.value) {
    case "End to end":
      dataToSave = getEndToEndProcessingTimeData();
      break;
    case "Partial":
      dataToSave = getPartialProcessingTimeData();
      break;
  }
  return dataToSave;
}

/** Builds end-to-end processing time rule data.
 * @returns {object} The rule data to submit.
 */
function getEndToEndProcessingTimeData() {
  let dataToSave;
  switch (selectedActSeqMore.value) {
    case "All":
      dataToSave = {
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "processing-time-end-to-end-whole",
      };
      isSubmitReset();
      isSubmitTimeCfmPtEteAll.value.rule = selectDurationTime.value;
      break;
    case "Start":
      dataToSave = {
        task: selectCfmPtEteStart.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "processing-time-end-to-end-starts-with",
      };
      isSubmitReset();
      isSubmitCfmPtEteStart.value = [
        { category: "Start", task: selectCfmPtEteStart.value },
      ];
      isSubmitTimeCfmPtEteStart.value.rule = selectDurationTime.value;
      break;
    case "End":
      dataToSave = {
        task: selectCfmPtEteEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "processing-time-end-to-end-ends-with",
      };
      isSubmitReset();
      isSubmitCfmPtEteEnd.value = [
        { category: "End", task: selectCfmPtEteEnd.value },
      ];
      isSubmitTimeCfmPtEteEnd.value.rule = selectDurationTime.value;
      break;
    case "Start & End":
      dataToSave = {
        start: selectCfmPtEteSEStart.value,
        end: selectCfmPtEteSEEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "processing-time-end-to-end-start-end",
      };
      isSubmitReset();
      isSubmitCfmPtEteSE.value = [
        { category: "Start", task: selectCfmPtEteSEStart.value },
        { category: "End", task: selectCfmPtEteSEEnd.value },
      ];
      isSubmitTimeCfmPtEteSE.value.rule = selectDurationTime.value;
      isSubmitShowDataPtEte.value = setSubmitShowDataByStartEnd(
        selectCfmPtEteSEStart.value,
        selectCfmPtEteSEEnd.value,
      );
      break;
  }
  return dataToSave;
}

/** Builds partial processing time rule data.
 * @returns {object} The rule data to submit.
 */
function getPartialProcessingTimeData() {
  let dataToSave;
  switch (selectedActSeqFromTo.value) {
    case "From":
      dataToSave = {
        task: selectCfmPtPStart.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "processing-time-partial-starts-with",
      };
      isSubmitReset();
      isSubmitCfmPtPStart.value = [
        { category: "From", task: selectCfmPtPStart.value },
      ];
      isSubmitTimeCfmPtPStart.value.rule = selectDurationTime.value;
      break;
    case "To":
      dataToSave = {
        task: selectCfmPtPEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "processing-time-partial-ends-with",
      };
      isSubmitReset();
      isSubmitCfmPtPEnd.value = [
        { category: "To", task: selectCfmPtPEnd.value },
      ];
      isSubmitTimeCfmPtPEnd.value.rule = selectDurationTime.value;
      break;
    case "From & To":
      dataToSave = {
        start: selectCfmPtPSEStart.value
          ? selectCfmPtPSEStart.value
          : activityRadioData.value.task[0],
        end: selectCfmPtPSEEnd.value
          ? selectCfmPtPSEEnd.value
          : activityRadioData.value.task[
              activityRadioData.value.task.length - 1
            ],
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "processing-time-partial-start-end",
      };
      isSubmitReset();
      isSubmitCfmPtPSE.value = [
        { category: "From", task: selectCfmPtPSEStart.value },
        { category: "To", task: selectCfmPtPSEEnd.value },
      ];
      isSubmitTimeCfmPtPSE.value.rule = selectDurationTime.value;
      isSubmitShowDataPtP.value = setSubmitShowDataByStartEnd(
        selectCfmPtPSEStart.value,
        selectCfmPtPSEEnd.value,
      );
      break;
  }
  return dataToSave;
}

/** Builds the data payload for the 'Waiting time' rule.
 * @returns {object} The rule data to submit.
 */
function getWaitingTimeData() {
  let dataToSave;
  switch (selectedProcessScope.value) {
    case "End to end":
      dataToSave = getEndToEndWaitingTimeData();
      break;
    case "Partial":
      dataToSave = getPartialWaitingTimeData();
      break;
  }
  return dataToSave;
}

/** Builds end-to-end waiting time rule data.
 * @returns {object} The rule data to submit.
 */
function getEndToEndWaitingTimeData() {
  let dataToSave;
  switch (selectedActSeqMore.value) {
    case "All":
      dataToSave = {
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "waiting-time-end-to-end-whole",
      };
      isSubmitReset();
      isSubmitTimeCfmWtEteAll.value.rule = selectDurationTime.value;
      break;
    case "Start":
      dataToSave = {
        task: selectCfmWtEteStart.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "waiting-time-end-to-end-starts-with",
      };
      isSubmitReset();
      isSubmitCfmWtEteStart.value = [
        { category: "Start", task: selectCfmWtEteStart.value },
      ];
      isSubmitTimeCfmWtEteStart.value.rule = selectDurationTime.value;
      break;
    case "End":
      dataToSave = {
        task: selectCfmWtEteEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "waiting-time-end-to-end-ends-with",
      };
      isSubmitReset();
      isSubmitCfmWtEteEnd.value = [
        { category: "End", task: selectCfmWtEteEnd.value },
      ];
      isSubmitTimeCfmWtEteEnd.value.rule = selectDurationTime.value;
      break;
    case "Start & End":
      dataToSave = {
        start: selectCfmWtEteSEStart.value,
        end: selectCfmWtEteSEEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "waiting-time-end-to-end-start-end",
      };
      isSubmitReset();
      isSubmitCfmWtEteSE.value = [
        { category: "Start", task: selectCfmWtEteSEStart.value },
        { category: "End", task: selectCfmWtEteSEEnd.value },
      ];
      isSubmitTimeCfmWtEteSE.value.rule = selectDurationTime.value;
      isSubmitShowDataWtEte.value = setSubmitShowDataByStartEnd(
        selectCfmWtEteSEStart.value,
        selectCfmWtEteSEEnd.value,
      );
      break;
  }
  return dataToSave;
}

/** Builds partial waiting time rule data.
 * @returns {object} The rule data to submit.
 */
function getPartialWaitingTimeData() {
  let dataToSave;
  switch (selectedActSeqFromTo.value) {
    case "From":
      dataToSave = {
        task: selectCfmWtPStart.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "waiting-time-partial-starts-with",
      };
      isSubmitReset();
      isSubmitCfmWtPStart.value = [
        { category: "From", task: selectCfmWtPStart.value },
      ];
      isSubmitTimeCfmWtPStart.value.rule = selectDurationTime.value;
      break;
    case "To":
      dataToSave = {
        task: selectCfmWtPEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "waiting-time-partial-ends-with",
      };
      isSubmitReset();
      isSubmitCfmWtPEnd.value = [
        { category: "To", task: selectCfmWtPEnd.value },
      ];
      isSubmitTimeCfmWtPEnd.value.rule = selectDurationTime.value;
      break;
    case "From & To":
      dataToSave = {
        start: selectCfmWtPSEStart.value,
        end: selectCfmWtPSEEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "waiting-time-partial-start-end",
      };
      isSubmitReset();
      isSubmitCfmWtPSE.value = [
        { category: "From", task: selectCfmWtPSEStart.value },
        { category: "To", task: selectCfmWtPSEEnd.value },
      ];
      isSubmitTimeCfmWtPSE.value.rule = selectDurationTime.value;
      isSubmitShowDataWtP.value = setSubmitShowDataByStartEnd(
        selectCfmWtPSEStart.value,
        selectCfmWtPSEEnd.value,
      );
      break;
  }
  return dataToSave;
}

/** Builds the data payload for the 'Cycle time' rule.
 * @returns {object} The rule data to submit.
 */
function getCycleTimeData() {
  let dataToSave;
  switch (selectedActSeqMore.value) {
    case "All":
      dataToSave = {
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "cycle-time-end-to-end-whole",
      };
      isSubmitReset();
      isSubmitTimeCfmCtEteAll.value.rule = selectDurationTime.value;
      break;
    case "Start":
      dataToSave = {
        task: selectCfmCtEteStart.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "cycle-time-end-to-end-starts-with",
      };
      isSubmitReset();
      isSubmitCfmCtEteStart.value = [
        { category: "Start", task: selectCfmCtEteStart.value },
      ];
      isSubmitTimeCfmCtEteStart.value.rule = selectDurationTime.value;
      break;
    case "End":
      dataToSave = {
        task: selectCfmCtEteEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "cycle-time-end-to-end-ends-with",
      };
      isSubmitReset();
      isSubmitCfmCtEteEnd.value = [
        { category: "End", task: selectCfmCtEteEnd.value },
      ];
      isSubmitTimeCfmCtEteEnd.value.rule = selectDurationTime.value;
      break;
    case "Start & End":
      dataToSave = {
        start: selectCfmCtEteSEStart.value,
        end: selectCfmCtEteSEEnd.value,
        min: selectDurationTime.value.min,
        max: selectDurationTime.value.max,
        type: "cycle-time-end-to-end-start-end",
      };
      isSubmitReset();
      isSubmitCfmCtEteSE.value = [
        { category: "Start", task: selectCfmCtEteSEStart.value },
        { category: "End", task: selectCfmCtEteSEEnd.value },
      ];
      isSubmitTimeCfmCtEteSE.value.rule = selectDurationTime.value;
      isSubmitShowDataCt.value = setSubmitShowDataByStartEnd(
        selectCfmCtEteSEStart.value,
        selectCfmCtEteSEEnd.value,
      );
      break;
  }
  return dataToSave;
}

/**
 * Sets up category-based task selection logic.
 */
function setTaskByCategoryOnRadioEmitting() {
  emitter.on("actRadioData", (data) => {
    const handlers = {
      cfmSeqStart: handleCfmSeqStart,
      cfmSeqEnd: handleCfmSeqEnd,
      cfmPtEteStart: (task) =>
        handleSimpleSelection("selectCfmPtEteStart", task),
      cfmPtEteEnd: (task) => handleSimpleSelection("selectCfmPtEteEnd", task),
      cfmPtEteSEStart: handleCfmPtEteSEStart,
      cfmPtEteSEEnd: handleCfmPtEteSEEnd,
      cfmPtPStart: (task) => handleSimpleSelection("selectCfmPtPStart", task),
      cfmPtPEnd: (task) => handleSimpleSelection("selectCfmPtPEnd", task),
      cfmPtPSEStart: handleCfmPtPSEStart,
      cfmPtPSEEnd: handleCfmPtPSEEnd,
      cfmWtEteStart: (task) =>
        handleSimpleSelection("selectCfmWtEteStart", task),
      cfmWtEteEnd: (task) => handleSimpleSelection("selectCfmWtEteEnd", task),
      cfmWtEteSEStart: handleCfmWtEteSEStart,
      cfmWtEteSEEnd: handleCfmWtEteSEEnd,
      cfmWtPStart: (task) => handleSimpleSelection("selectCfmWtPStart", task),
      cfmWtPEnd: (task) => handleSimpleSelection("selectCfmWtPEnd", task),
      cfmWtPSEStart: handleCfmWtPSEStart,
      cfmWtPSEEnd: handleCfmWtPSEEnd,
      cfmCtEteStart: (task) =>
        handleSimpleSelection("selectCfmCtEteStart", task),
      cfmCtEteEnd: (task) => handleSimpleSelection("selectCfmCtEteEnd", task),
      cfmCtEteSEStart: handleCfmCtEteSEStart,
      cfmCtEteSEEnd: handleCfmCtEteSEEnd,
    };

    if (handlers[data.category]) {
      handlers[data.category](data.task);
    } else if (selectedRuleType.value === "Activity duration") {
      selectDurationData.value = [data.task];
    }
  });
}

/**
 * Handles start task selection for activity sequence.
 * @param {string} task - The selected task.
 */
function handleCfmSeqStart(task) {
  if (isStartSelected.value && task !== selectCfmSeqStart.value) {
    selectCfmSeqEnd.value = null;
  }
  selectCfmSeqStart.value = task;
}

/**
 * Handles end task selection for activity sequence.
 * @param {string} task - The selected task.
 */
function handleCfmSeqEnd(task) {
  if (isEndSelected.value && task !== selectCfmSeqEnd.value) {
    selectCfmSeqStart.value = null;
  }
  selectCfmSeqEnd.value = task;
}

/**
 * Sets a simple task selection on a ref field.
 * @param {object} field - The ref to set.
 * @param {string} task - The selected task.
 */
function handleSimpleSelection(field, task) {
  selectFieldRefs[field].value = task;
}

/**
 * Handles start task for end-to-end processing time.
 * @param {string} task - The selected task.
 */
function handleCfmPtEteSEStart(task) {
  if (isStartSelected.value && task !== selectCfmPtEteSEStart.value) {
    selectCfmPtEteSEEnd.value = null;
  }
  selectCfmPtEteSEStart.value = task;
}
/**
 * Handles end task for end-to-end processing time.
 * @param {string} task - The selected task.
 */
function handleCfmPtEteSEEnd(task) {
  if (isEndSelected.value && task !== selectCfmPtEteSEEnd.value) {
    selectCfmPtEteSEStart.value = null;
  }
  selectCfmPtEteSEEnd.value = task;
}
/**
 * Handles start task for partial processing time.
 * @param {string} task - The selected task.
 */
function handleCfmPtPSEStart(task) {
  if (isStartSelected.value && task !== selectCfmPtPSEStart.value) {
    selectCfmPtPSEEnd.value = null;
  }
  selectCfmPtPSEStart.value = task;
}
/**
 * Handles end task for partial processing time.
 * @param {string} task - The selected task.
 */
function handleCfmPtPSEEnd(task) {
  if (isEndSelected.value && task !== selectCfmPtPSEEnd.value) {
    selectCfmPtPSEStart.value = null;
  }
  selectCfmPtPSEEnd.value = task;
}
/**
 * Handles start task for end-to-end waiting time.
 * @param {string} task - The selected task.
 */
function handleCfmWtEteSEStart(task) {
  if (isStartSelected.value && task !== selectCfmWtEteSEStart.value) {
    selectCfmWtEteSEEnd.value = null;
  }
  selectCfmWtEteSEStart.value = task;
}
/**
 * Handles end task for end-to-end waiting time.
 * @param {string} task - The selected task.
 */
function handleCfmWtEteSEEnd(task) {
  if (isEndSelected.value && task !== selectCfmWtEteSEEnd.value) {
    selectCfmWtEteSEStart.value = null;
  }
  selectCfmWtEteSEEnd.value = task;
}
/**
 * Handles start task for partial waiting time.
 * @param {string} task - The selected task.
 */
function handleCfmWtPSEStart(task) {
  if (isStartSelected.value && task !== selectCfmWtPSEStart.value) {
    selectCfmWtPSEEnd.value = null;
  }
  selectCfmWtPSEStart.value = task;
}
/**
 * Handles end task for partial waiting time.
 * @param {string} task - The selected task.
 */
function handleCfmWtPSEEnd(task) {
  if (isEndSelected.value && task !== selectCfmWtPSEEnd.value) {
    selectCfmWtPSEStart.value = null;
  }
  selectCfmWtPSEEnd.value = task;
}
/**
 * Handles start task for end-to-end cycle time.
 * @param {string} task - The selected task.
 */
function handleCfmCtEteSEStart(task) {
  if (isStartSelected.value && task !== selectCfmCtEteSEStart.value) {
    selectCfmCtEteSEEnd.value = null;
  }
  selectCfmCtEteSEStart.value = task;
}
/**
 * Handles end task for end-to-end cycle time.
 * @param {string} task - The selected task.
 */
function handleCfmCtEteSEEnd(task) {
  if (isEndSelected.value && task !== selectCfmCtEteSEEnd.value) {
    selectCfmCtEteSEStart.value = null;
  }
  selectCfmCtEteSEEnd.value = task;
}

/** Sets task data when the list sequence emits a change. */
function setTaskByCategoryOnListSeqEmitting() {
  emitter.on("getListSequence", (data) => {
    switch (data.category) {
      case "cfmSeqDirectly":
        selectCfmSeqDirectly.value = data.task;
        break;
      case "cfmSeqEventually":
        selectCfmSeqEventually.value = data.task;
        break;
      default:
        break;
    }
  });
}

/** Checks if the 'Have activity' rule has valid selections.
 * @returns {boolean} Whether the button should be disabled.
 */
function checkHaveActivity() {
  return !selectConformanceTask.value?.length;
}
/** Checks if the 'Activity duration' rule has valid selections.
 * @returns {boolean} Whether the button should be disabled.
 */
function checkActivityDuration() {
  return !selectDurationData.value?.length;
}
/**
 * Checks the activity sequence logic.
 * @returns {boolean} Whether the button should be disabled.
 */
function checkActivitySequence() {
  switch (selectedActivitySequence.value) {
    case "Start & End":
      return checkStartAndEndSequence(
        selectCfmSeqStart.value,
        selectCfmSeqEnd.value,
      );
    case "Sequence":
      return checkSequenceMode(selectedMode.value);
    default:
      return true;
  }
}
/**
 * Checks the Start & End activity sequence.
 * @param {string} start - The start activity.
 * @param {string} end - The end activity.
 * @returns {boolean} Whether the button should be disabled.
 */
function checkStartAndEndSequence(start, end) {
  return !(start && end);
}

/** Checks if the 'Processing time' rule has valid selections.
 * @returns {boolean} Whether the button should be disabled.
 */
function checkProcessingTime() {
  let disabled = true;
  switch (selectedProcessScope.value) {
    case "End to end":
      disabled = checkEndToEnd();
      break;
    case "Partial":
      disabled = checkPartial();
      break;
    default:
      break;
  }
  return disabled;
}
/** Checks end-to-end scope selections.
 * @returns {boolean} Whether the button should be disabled.
 */
function checkEndToEnd() {
  let disabled = true;
  switch (selectedActSeqMore.value) {
    case "All":
      disabled = false;
      break;
    case "Start":
      disabled = !selectCfmPtEteStart.value;
      break;
    case "End":
      disabled = !selectCfmPtEteEnd.value;
      break;
    case "Start & End":
      disabled = checkStartAndEnd(
        selectCfmPtEteSEStart.value,
        selectCfmPtEteSEEnd.value,
      );
      break;
    default:
      break;
  }
  return disabled;
}
/** Checks partial scope selections.
 * @returns {boolean} Whether the button should be disabled.
 */
function checkPartial() {
  let disabled = true;
  switch (selectedActSeqFromTo.value) {
    case "From":
      disabled = !selectCfmPtPStart.value;
      break;
    case "To":
      disabled = !selectCfmPtPEnd.value;
      break;
    case "From & To":
      disabled = checkStartAndEnd(
        selectCfmPtPSEStart.value,
        selectCfmPtPSEEnd.value,
      );
      break;
    default:
      break;
  }
  return disabled;
}
function checkWaitingTime() {
  let disabled = true;
  switch (selectedProcessScope.value) {
    case "End to end":
      disabled = checkEndToEndWaiting();
      break;
    case "Partial":
      disabled = checkPartialWaiting();
      break;
    default:
      break;
  }
  return disabled;
}

function checkEndToEndWaiting() {
  let disabled = true;
  switch (selectedActSeqMore.value) {
    case "Start":
      disabled = !selectCfmWtEteStart.value;
      break;
    case "End":
      disabled = !selectCfmWtEteEnd.value;
      break;
    case "Start & End":
      disabled = selectCfmWtEteSEStart.value !== selectCfmWtEteSEEnd.value;
      break;
    default:
      break;
  }
  return disabled;
}
function checkPartialWaiting() {
  let disabled = true;
  switch (selectedActSeqFromTo.value) {
    case "From":
      disabled = !selectCfmWtPStart.value;
      break;
    case "To":
      disabled = !selectCfmWtPEnd.value;
      break;
    case "From & To":
      disabled = checkStartAndEnd(
        selectCfmWtPSEStart.value,
        selectCfmWtPSEEnd.value,
      );
      break;
    default:
      break;
  }
  return disabled;
}
function checkCycleTime() {
  let disabled = true;
  switch (selectedActSeqMore.value) {
    case "All":
      disabled = false;
      break;
    case "Start":
      disabled = !selectCfmCtEteStart.value;
      break;
    case "End":
      disabled = !selectCfmCtEteEnd.value;
      break;
    case "Start & End":
      disabled = checkStartAndEnd(
        selectCfmCtEteSEStart.value,
        selectCfmCtEteSEEnd.value,
      );
      break;
    default:
      break;
  }
  return disabled;
}
function checkStartAndEnd(start, end) {
  return isAlreadySubmit.value ? start !== end : !(start && end);
}

// created() logic
isShowBarOpen.value = !(
  conformanceLogCreateCheckId.value || conformanceFilterCreateCheckId.value
);
// Result of list selection
emitter.on("actListData", (data) => {
  selectConformanceTask.value = data;
});

setTaskByCategoryOnRadioEmitting();
setTaskByCategoryOnListSeqEmitting();

// Clear data when radio selection changes
emitter.on("isRadioChange", (data) => {
  if (data) {
    selectTimeReset();
    selectDurationTime.value = { min: 0, max: 0 };
  }
});
emitter.on("isRadioSeqChange", (data) => {
  if (data) {
    selectTimeReset();
  }
});
emitter.on("isRadioProcessScopeChange", (data) => {
  if (data) {
    selectTimeReset();
    selectDurationTime.value = { min: 0, max: 0 };
  }
});
emitter.on("isRadioActSeqMoreChange", (data) => {
  if (data) {
    selectTimeReset();
    if (selectedActSeqMore.value !== "All")
      selectDurationTime.value = { min: 0, max: 0 };
  }
});
emitter.on("isRadioActSeqFromToChange", (data) => {
  if (data) {
    selectTimeReset();
    selectDurationTime.value = { min: 0, max: 0 };
  }
});

// beforeUnmount
onBeforeUnmount(() => {
  if (reportWatchTimerId) clearTimeout(reportWatchTimerId);
  emitter.off("actRadioData");
  emitter.off("getListSequence");
  emitter.off("actListData");
  emitter.off("isRadioChange");
  emitter.off("isRadioSeqChange");
  emitter.off("isRadioProcessScopeChange");
  emitter.off("isRadioActSeqMoreChange");
  emitter.off("isRadioActSeqFromToChange");
  isSubmitReset();
  selectTimeReset();
});
</script>
<style>
.text-shadow {
  text-shadow: 1.5px 1px 8px black;
}
</style>
