import "../../../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                                */
/* empty css                                                                    */
/* empty css                                                                     */
/* empty css                                                                  */
/* empty css                                                                        */
/* empty css                                                                       */
/* empty css                                                                    */
/* empty css                                                                             */
/* empty css                                                                       */
import { inject, ref, computed, watch, nextTick, createBlock, openBlock, mergeProps, toHandlers } from "vue";
import { ElCascader } from "../../../../../../node_modules/element-plus/es/components/cascader/index.mjs";
const _sfc_main = /* @__PURE__ */ Object.assign({
  inheritAttrs: false
}, {
  __name: "Cascader",
  props: {
    config: {
      type: Object,
      required: true
    },
    modelValue: {
      type: null,
      default: ""
    },
    placeholder: {
      type: String,
      default: "请选择"
    },
    disabled: {
      type: Boolean,
      default: false
    },
    readonly: {
      type: Boolean,
      default: false
    },
    optionConfig: {
      type: Object,
      default: () => ({})
    },
    multiple: {
      type: Boolean,
      default: false
    },
    showFullPath: {
      type: Boolean,
      default: true
    },
    maxLevel: {
      type: Number,
      default: 10
    },
    propsData: {
      type: Object,
      default: () => ({})
    },
    checkStrictly: {
      type: Boolean,
      default: false
    }
  },
  emits: ["update:modelValue", "change"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const formData = inject("formData");
    const emit = __emit;
    const cascaderRef = ref(null);
    const propsDataValue = computed(() => {
      return {
        ...props.propsData,
        multiple: props.multiple,
        checkStrictly: props.checkStrictly
      };
    });
    const inputValue = computed({
      get: () => {
        return props.modelValue;
      },
      set: (val) => {
        emit("update:modelValue", val);
        emit("change", val);
      }
    });
    watch(inputValue, (val) => {
      if (props.config.displayFieldName && val) {
        nextTick(() => {
          const displayValue = getDisplayPath();
          if (formData && displayValue) {
            formData.value[props.config.displayFieldName] = displayValue;
          }
        });
      }
    });
    const getDisplayPath = () => {
      let checkedNodes = cascaderRef.value.getCheckedNodes();
      let textArr = [];
      checkedNodes.forEach((item) => {
        let label;
        if (props.showFullPath) {
          label = item.text;
        } else {
          let pathLabels = item.pathLabels;
          label = pathLabels[pathLabels.length - 1];
        }
        if (label) {
          textArr.push(label);
        }
      });
      return textArr.join(" / ");
    };
    const options = computed(() => {
      if (!props.optionConfig) return [];
      const optionList = props.optionConfig.options || [];
      if (optionList.length > 0) {
        return flatToTree(
          optionList,
          props.optionConfig.idField,
          props.optionConfig.pidField,
          props.maxLevel
        );
      }
      return optionList;
    });
    function flatToTree(list, idKey = "id", pidKey = "pid", maxLevel = 10) {
      const data = JSON.parse(JSON.stringify(list));
      const nodeMap = {};
      data.forEach((node) => {
        nodeMap[node[idKey]] = node;
        node.children = node.children || [];
        node.level = 1;
      });
      const tree = [];
      data.forEach((node) => {
        const parentId = node[pidKey];
        const parentNode = nodeMap[parentId];
        if (parentNode && parentNode.level < maxLevel) {
          node.level = parentNode.level + 1;
          parentNode.children.push(node);
        } else {
          tree.push(node);
        }
      });
      return tree;
    }
    return (_ctx, _cache) => {
      const _component_el_cascader = ElCascader;
      return openBlock(), createBlock(_component_el_cascader, mergeProps({
        modelValue: inputValue.value,
        "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => inputValue.value = $event)
      }, toHandlers(__props.config.eventHandlers), {
        options: options.value,
        props: propsDataValue.value,
        ref_key: "cascaderRef",
        ref: cascaderRef,
        clearable: "",
        filterable: "",
        disabled: __props.disabled,
        placeholder: __props.placeholder,
        "show-all-levels": __props.showFullPath,
        style: [{ "width": "100%" }, {
          pointerEvents: __props.readonly ? "none" : "auto"
        }]
      }), null, 16, ["modelValue", "options", "props", "disabled", "placeholder", "show-all-levels", "style"]);
    };
  }
});
export {
  _sfc_main as default
};
