"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
;/* empty css                                               */
;/* empty css                                                */
;/* empty css                                                */
;/* empty css                                               */
;/* empty css                                               */
const Vue = require("vue");
;/* empty css                     */
const _pluginVue_exportHelper = require("../../../../../../_virtual/_plugin-vue_export-helper.js");
const index = require("../../../../node_modules/vant/es/steps/index.js");
const index$1 = require("../../../../node_modules/vant/es/step/index.js");
const index$2 = require("../../../../node_modules/vant/es/icon/index.js");
const _hoisted_1 = { class: "time-line-filter" };
const _hoisted_2 = { class: "line-selected-content" };
const _hoisted_3 = { class: "custom-item" };
const _hoisted_4 = { class: "line-list-content" };
const _hoisted_5 = ["onClick"];
const _sfc_main = {
  __name: "TimeLineFilter",
  props: {
    data: {
      type: Array,
      default: []
    },
    option: {
      type: Object,
      default: {
        id: "id",
        name: "name",
        pid: "pid",
        isLeaf: "isLeaf"
      }
    },
    modelValue: {
      type: String,
      default: ""
    }
  },
  emits: ["update:modelValue"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const selectedList = Vue.ref([]);
    const targetId = Vue.ref("0");
    const listDataActId = Vue.ref("");
    let isDown = true;
    Vue.watch(
      [() => props.modelValue, () => props.data],
      ([value, data]) => {
        if (value && data.length > 0 && isDown) {
          debugger;
          const ids = value.split(",");
          selectedList.value = data.filter((item) => ids.includes(item.id + ""));
          selectedList.value = selectedList.value.map((s) => {
            s.isAct = false;
            return s;
          });
          const lastNode = selectedList.value[selectedList.value.length - 1];
          const maxLevel = getMaxLevel(data);
          if (ids.length < maxLevel && (lastNode == null ? void 0 : lastNode.isLeaf) != true) {
            targetId.value = ids[ids.length - 1];
            selectedList.value.push({
              id: "pleaseSelect",
              name: "请选择",
              hollow: true,
              isAct: true,
              pid: targetId.value
            });
          } else {
            selectedList.value[selectedList.value.length - 1].isAct = true;
            targetId.value = ids[ids.length - 2];
          }
          listDataActId.value = selectedList.value[selectedList.value.length - 1].id;
        }
      },
      {
        immediate: true
      }
    );
    function getMaxLevel(list) {
      const map = {};
      let maxLevel = 0;
      list.forEach((item) => {
        map[item.id] = { pid: item.pid, lvl: 0 };
      });
      list.forEach((item) => {
        let currentId = item.id;
        let level = 1;
        while (map[currentId] && map[currentId].pid != "0") {
          currentId = map[currentId].pid;
          level++;
        }
        map[item.id].lvl = level;
        if (level > maxLevel) {
          maxLevel = level;
        }
      });
      return maxLevel;
    }
    function getChildrenById(tree, targetId2) {
      if (!targetId2 || targetId2 == 0) {
        return tree;
      }
      function findNode(node) {
        if (node.id == targetId2) {
          return node.children;
        }
        for (const child of node.children) {
          const result = findNode(child);
          if (result) {
            return result;
          }
        }
        return null;
      }
      for (const root of tree) {
        const children = findNode(root);
        if (children) {
          return children;
        }
      }
      return null;
    }
    function getNodeById(tree, id) {
      if (Array.isArray(tree)) {
        for (let i = 0; i < tree.length; i++) {
          const node = tree[i];
          if (node.id === id) {
            return node;
          }
          if (node.children && node.children.length > 0) {
            const found = getNodeById(node.children, id);
            if (found) {
              return found;
            }
          }
        }
      }
      return null;
    }
    const treeData = Vue.computed(() => {
      return listToTree(props.data);
    });
    const listData = Vue.computed(() => {
      return getChildrenById(treeData.value, targetId.value);
    });
    function clickListItem(item) {
      isDown = true;
      if (selectedList.value.length >= item.lvl) {
        selectedList.value = selectedList.value.slice(0, item.lvl - 1);
      }
      selectedList.value.push(item);
      const selectedIdList = selectedList.value.map((s) => s.id);
      emit("update:modelValue", "");
      Vue.nextTick(() => {
        emit("update:modelValue", selectedIdList.join(","));
      });
    }
    function clickSelectedItem(item) {
      if (item.id == "pleaseSelect") {
        const t = getNodeById(treeData.value, item.pid);
        clickListItem(t);
        return;
      }
      isDown = false;
      targetId.value = item.pid;
      const index2 = selectedList.value.findIndex((t) => t.id === item.id);
      if (index2 !== -1) {
        selectedList.value.splice(index2 + 1);
      }
      listDataActId.value = item.id;
      selectedList.value.forEach((s) => {
        s.isAct = false;
        if (item.id == s.id) {
          s.isAct = true;
        }
      });
      const selectedIdList = selectedList.value.map((s) => s.id);
      const maxLevel = getMaxLevel(props.data);
      if (selectedIdList.length < maxLevel) {
        selectedList.value.push({
          id: "pleaseSelect",
          name: "请选择",
          hollow: true,
          isAct: false,
          pid: item.id
        });
      }
      emit("update:modelValue", "");
      Vue.nextTick(() => {
        emit("update:modelValue", selectedIdList.join(","));
      });
    }
    function listToTree(list) {
      const map = {};
      const tree = [];
      list = list.map((item) => {
        const temp = {
          id: item[props.option.id],
          name: item[props.option.name],
          pid: item[props.option.pid],
          ...item
        };
        return temp;
      });
      list.forEach((item) => {
        map[item.id] = { ...item, children: [], lvl: 0 };
      });
      list.forEach((item) => {
        const node = map[item.id];
        const parentId = item.pid;
        if (parentId == "0") {
          node.lvl = 1;
          tree.push(node);
        } else {
          const parent = map[parentId];
          if (parent) {
            node.lvl = parent.lvl + 1;
            parent.children.push(node);
          }
        }
      });
      return tree;
    }
    return (_ctx, _cache) => {
      const _component_van_icon = index$2.Icon;
      const _component_van_step = index$1.Step;
      const _component_van_steps = index.Steps;
      return Vue.openBlock(), Vue.createElementBlock("div", _hoisted_1, [
        Vue.createElementVNode("div", _hoisted_2, [
          Vue.unref(selectedList).length > 0 ? (Vue.openBlock(), Vue.createBlock(_component_van_steps, {
            key: 0,
            direction: "vertical",
            class: "selected-time-line"
          }, {
            default: Vue.withCtx(() => [
              (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(Vue.unref(selectedList), (item, index2) => {
                return Vue.openBlock(), Vue.createBlock(_component_van_step, {
                  onClick: ($event) => clickSelectedItem(item)
                }, {
                  default: Vue.withCtx(() => [
                    Vue.createElementVNode("div", _hoisted_3, [
                      Vue.createElementVNode("span", {
                        class: Vue.normalizeClass(["label", { act: item.isAct || item.id == Vue.unref(listDataActId) }])
                      }, Vue.toDisplayString(item.name), 3),
                      item.isAct || item.id == Vue.unref(listDataActId) ? (Vue.openBlock(), Vue.createBlock(_component_van_icon, {
                        key: 0,
                        name: "arrow",
                        color: "#ccc"
                      })) : Vue.createCommentVNode("", true)
                    ])
                  ]),
                  _: 2
                }, 1032, ["onClick"]);
              }), 256))
            ]),
            _: 1
          })) : Vue.createCommentVNode("", true)
        ]),
        Vue.createElementVNode("div", _hoisted_4, [
          (Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(Vue.unref(listData), (item) => {
            return Vue.openBlock(), Vue.createElementBlock("div", {
              class: Vue.normalizeClass(["line-list-item", { act: item.id == Vue.unref(listDataActId) }]),
              onClick: ($event) => clickListItem(item)
            }, [
              Vue.createElementVNode("span", null, Vue.toDisplayString(item.name), 1),
              item.id == Vue.unref(listDataActId) ? (Vue.openBlock(), Vue.createBlock(_component_van_icon, {
                key: 0,
                name: "success"
              })) : Vue.createCommentVNode("", true)
            ], 10, _hoisted_5);
          }), 256))
        ])
      ]);
    };
  }
};
const _TimeLineFilter = /* @__PURE__ */ _pluginVue_exportHelper.default(_sfc_main, [["__scopeId", "data-v-9904532b"]]);
exports.default = _TimeLineFilter;
