import { defineComponent, useModel, resolveComponent, openBlock, createBlock, withCtx, createElementVNode, toDisplayString, createElementBlock, createCommentVNode, mergeModels } from 'vue';

const _hoisted_1 = { key: 0 };
var _sfc_main = /* @__PURE__ */ defineComponent({
  ...{
    name: "FmCascader"
  },
  __name: "index",
  props: /* @__PURE__ */ mergeModels({
    orgData: {
      type: Array,
      default: () => []
    },
    cascaderProps: {
      type: Object,
      default: () => ({ emitPath: false, value: "id", label: "name", expandTrigger: "hover" })
    },
    placeholder: {
      type: String,
      default: "\u8BF7\u9009\u62E9\u7236\u8282"
    },
    vId: {
      type: String,
      default: "opOrgId"
    },
    vCmpId: {
      type: String,
      default: "opCmpId"
    },
    vDetId: {
      type: String,
      default: "opDetId"
    },
    vGroupId: {
      type: String,
      default: "opGroupId"
    }
  }, {
    "modelValue": {},
    "modelModifiers": {}
  }),
  emits: /* @__PURE__ */ mergeModels(["change"], ["update:modelValue"]),
  setup(__props, { emit: __emit }) {
    const modeValue = useModel(__props, "modelValue");
    const emit = __emit;
    const props = __props;
    const handelchangeOrg = (val) => {
      modeValue.value[props.vId] = val;
      const node = findNode(val);
      if (node) {
        if (node.pid == 0) {
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vCmpId))
            modeValue.value[props.vCmpId] = node.id;
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vDetId))
            modeValue.value[props.vDetId] = 0;
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vGroupId))
            modeValue.value[props.vGroupId] = 0;
        } else if (node.level == 2) {
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vDetId))
            modeValue.value[props.vDetId] = node.id;
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vCmpId))
            modeValue.value[props.vCmpId] = node.pid;
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vGroupId))
            modeValue.value[props.vGroupId] = 0;
        } else if (node.level == 3) {
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vGroupId))
            modeValue.value[props.vGroupId] = node.id;
          if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vDetId))
            modeValue.value[props.vDetId] = node.pid;
          let top = findNode(node.pid);
          if (top) {
            if (Object.prototype.hasOwnProperty.call(modeValue.value, props.vCmpId))
              modeValue.value[props.vCmpId] = top.id;
          }
        }
      }
      emit("change", node);
    };
    const findNode = (id) => {
      const search = (nodes) => {
        var _a;
        for (const node of nodes) {
          if (node.id === id) return node;
          if ((_a = node.children) == null ? void 0 : _a.length) {
            const found = search(node.children);
            if (found) return found;
          }
        }
        return null;
      };
      return search(props.orgData || []);
    };
    return (_ctx, _cache) => {
      const _component_el_cascader = resolveComponent("el-cascader");
      return openBlock(), createBlock(_component_el_cascader, {
        options: props.orgData,
        props: __props.cascaderProps,
        placeholder: __props.placeholder,
        filterable: "",
        class: "w100",
        modelValue: modeValue.value[__props.vId],
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => modeValue.value[__props.vId] = $event),
        onChange: handelchangeOrg
      }, {
        default: withCtx(({ node, data }) => [
          createElementVNode(
            "span",
            null,
            toDisplayString(node.label),
            1
            /* TEXT */
          ),
          !node.isLeaf ? (openBlock(), createElementBlock(
            "span",
            _hoisted_1,
            " (" + toDisplayString(data.children.length) + ") ",
            1
            /* TEXT */
          )) : createCommentVNode("v-if", true)
        ]),
        _: 1
        /* STABLE */
      }, 8, ["options", "props", "placeholder", "modelValue"]);
    };
  }
});

export { _sfc_main as default };
