import "../../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                          */
/* empty css                                                                  */
/* empty css                                                               */
/* empty css                                                            */
/* empty css                                                               */
/* empty css                                                                     */
/* empty css                                                               */
import "../../../../node_modules/element-plus/theme-chalk/el-tooltip.css.mjs";
/* empty css                                                                  */
/* empty css                                                              */
/* empty css                                                               */
/* empty css                                                                          */
import "../../../../node_modules/element-plus/theme-chalk/el-date-picker.css.mjs";
import { getCurrentInstance, computed, watch, ref, createElementBlock, unref, openBlock, createBlock, isRef, createVNode, createElementVNode, withCtx, Fragment, renderList } from "vue";
import { useVModel } from "../../../../node_modules/@vueuse/core/dist/index.mjs";
import ValueSelector from "../../components/ValueSelector.vue.mjs";
import api from "../../api/index.mjs";
import { getEditConfigDataSources } from "../utils/common.mjs";
import { createDataSource } from "../utils/datasource.mjs";
/* empty css                */
import _export_sfc from "../../../../_virtual/_plugin-vue_export-helper.mjs";
import { ElInput } from "../../../../node_modules/element-plus/es/components/input/index.mjs";
import { ElDatePicker } from "../../../../node_modules/element-plus/es/components/date-picker/index.mjs";
import { ElInputTag } from "../../../../node_modules/element-plus/es/components/input-tag/index.mjs";
import { ElSelect, ElOption } from "../../../../node_modules/element-plus/es/components/select/index.mjs";
const _hoisted_1 = { key: 0 };
const _hoisted_2 = {
  key: 1,
  class: "value-input-wrapper"
};
const _hoisted_3 = {
  key: 1,
  class: "range-input"
};
const _hoisted_4 = {
  key: 2,
  class: "normal-input-wrapper"
};
const _hoisted_5 = {
  key: 0,
  class: "range-input"
};
const _hoisted_6 = {
  key: 1,
  class: "range-input"
};
const _sfc_main = {
  __name: "ValueInput",
  props: {
    modelValue: [String, Number, Array, null],
    fieldType: String,
    operator: String,
    field: Object,
    allFields: Array,
    isField: Boolean,
    showText: String,
    size: String
  },
  emits: [
    "update:modelValue",
    "update:isField",
    "update:showText"
  ],
  setup(__props, { emit: __emit }) {
    const currentInstance = getCurrentInstance();
    const ctx = currentInstance.proxy;
    const $props = __props;
    const $emits = __emit;
    const data = computed({
      get() {
        const val = $props.modelValue;
        return val;
      },
      set(val) {
        $emits("update:modelValue", val);
      }
    });
    const isSelectField = useVModel($props, "isField", $emits);
    watch(isSelectField, (newVal, oldVal) => {
      if (newVal !== oldVal) {
        data.value = null;
      }
    });
    const isMultiple = ref(false);
    watch(
      () => $props.operator,
      (newVal) => {
        const multiOperators = ["in_any", "ni_any", "contains", "not_contains"];
        isMultiple.value = multiOperators.includes(newVal);
      },
      { immediate: true }
    );
    watch(
      () => $props.operator,
      (newOperator, oldOperator) => {
        const wasMultiple = [
          "in_any",
          "ni_any",
          "contains",
          "not_contains"
        ].includes(oldOperator);
        const isNowMultiple = [
          "in_any",
          "ni_any",
          "contains",
          "not_contains"
        ].includes(newOperator);
        const wasRange = oldOperator === "range";
        const isNowRange = newOperator === "range";
        if (wasRange !== isNowRange) {
          data.value = null;
        } else if (!wasMultiple && isNowMultiple && data.value !== null) {
          if (!Array.isArray(data.value)) {
            data.value = [data.value];
          }
        } else if (wasMultiple && !isNowMultiple && Array.isArray(data.value)) {
          const newValue = data.value.length > 0 ? data.value[0] : null;
          data.value = newValue;
        }
      }
    );
    const isRange = computed(() => {
      return $props.operator === "range";
    });
    const needInput = computed(() => {
      const nullOperators = ["is_null", "is_not_null"];
      return !nullOperators.includes($props.operator);
    });
    const isTagInput = computed(() => {
      return $props.fieldType === "input" && isMultiple.value && !isSelectField.value;
    });
    const rangeMin = computed({
      get: () => {
        let val = data.value;
        if (val && typeof val === "object" && val.type === "field") {
          val = val.value;
        }
        if (Array.isArray(val)) {
          return val[0];
        }
        return "";
      },
      set: (val) => {
        let currentVal = data.value;
        if (currentVal && typeof currentVal === "object" && currentVal.type === "field") {
          currentVal = currentVal.value;
        }
        const max = Array.isArray(currentVal) ? currentVal[1] : "";
        if (val === "" && max === "") {
          data.value = null;
        } else {
          const shouldConvertToNumber = $props.fieldType === "number" && !isSelectField.value;
          data.value = [
            val === "" ? null : shouldConvertToNumber ? Number(val) : val,
            max === "" ? null : shouldConvertToNumber ? Number(max) : max
          ];
        }
      }
    });
    const rangeMax = computed({
      get: () => {
        let val = data.value;
        if (val && typeof val === "object" && val.type === "field") {
          val = val.value;
        }
        if (Array.isArray(val) && val.length > 1) {
          return val[1];
        }
        return "";
      },
      set: (val) => {
        let currentVal = data.value;
        if (currentVal && typeof currentVal === "object" && currentVal.type === "field") {
          currentVal = currentVal.value;
        }
        const min = Array.isArray(currentVal) ? currentVal[0] : "";
        if (min === "" && val === "") {
          data.value = null;
        } else {
          const shouldConvertToNumber = $props.fieldType === "number" && !isSelectField.value;
          data.value = [
            min === "" ? null : shouldConvertToNumber ? Number(min) : min,
            val === "" ? null : shouldConvertToNumber ? Number(val) : val
          ];
        }
      }
    });
    const selectOptions = ref([]);
    const dataSources = computed(() => {
      return getEditConfigDataSources();
    });
    watch(
      () => $props.field,
      (field) => {
        const fieldType = field.fieldType;
        if ((fieldType === "select" || fieldType === "multipleSelection" || fieldType === "radio" || fieldType === "checkbox") && field.optionConfig) {
          const {
            optionSource,
            dataSourceValue,
            dictionaryValue,
            options,
            displayField,
            valueField
          } = field.optionConfig;
          if (optionSource === "dataSource" && dataSourceValue) {
            try {
              const dsManager = createDataSource({
                http: ctx.$http,
                dataSources: dataSources.value,
                currentDataSourceId: dataSourceValue
              });
              dsManager.fetch().then((data2) => {
                var _a, _b;
                let options2 = [];
                if (Array.isArray(data2)) {
                  options2 = data2;
                } else if (typeof data2 === "object") {
                  options2 = Array.isArray(data2.data) ? data2.data : ((_a = data2.data) == null ? void 0 : _a.records) || ((_b = data2.data) == null ? void 0 : _b.list) || [];
                }
                options2 = options2.map((item) => ({
                  label: item[displayField],
                  value: item[valueField]
                }));
                selectOptions.value = options2;
                dsManager.destroy();
              });
            } catch (error) {
              console.error(`Failed to load data source ${dataSourceId}:`, error);
            }
          } else if (optionSource === "dictionary" && dictionaryValue) {
            api.getDicDataByKey(ctx.$http, dictionaryValue).then((data2) => {
              const result = [];
              for (const key in data2) {
                result.push({ label: data2[key], value: key });
              }
              selectOptions.value = result;
            });
          } else {
            selectOptions.value = options || [];
          }
        }
      },
      { deep: true, immediate: true }
    );
    const showText = computed(() => {
      if (!data.value && data.value != 0) return "";
      if ($props.fieldType === "select") {
        const options = selectOptions.value || [];
        if (isMultiple.value && Array.isArray(data.value)) {
          const labels = data.value.map((val) => {
            const option = options.find((opt) => opt.value === val);
            return option ? option.label : val;
          });
          return labels.join(", ");
        } else {
          const option = options.find((opt) => opt.value === data.value);
          return option ? option.label : data.value;
        }
      }
      if ($props.fieldType === "boolean") {
        return data.value === true ? "是" : data.value === false ? "否" : "";
      }
      return String(data.value);
    });
    watch(
      showText,
      (newVal) => {
        $emits("update:showText", newVal);
      },
      { immediate: true, deep: true }
    );
    return (_ctx, _cache) => {
      const _component_el_input = ElInput;
      const _component_el_date_picker = ElDatePicker;
      const _component_el_input_tag = ElInputTag;
      const _component_el_option = ElOption;
      const _component_el_select = ElSelect;
      return !unref(needInput) ? (openBlock(), createElementBlock("div", _hoisted_1)) : (openBlock(), createElementBlock("div", _hoisted_2, [
        unref(isSelectField) && !unref(isRange) ? (openBlock(), createBlock(ValueSelector, {
          key: 0,
          modelValue: unref(data),
          "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => isRef(data) ? data.value = $event : null),
          multiple: unref(isMultiple),
          placeholder: "请选择字段",
          style: { "width": "100%" },
          size: __props.size
        }, null, 8, ["modelValue", "multiple", "size"])) : unref(isSelectField) && unref(isRange) ? (openBlock(), createElementBlock("div", _hoisted_3, [
          createVNode(ValueSelector, {
            modelValue: unref(rangeMin),
            "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => isRef(rangeMin) ? rangeMin.value = $event : null),
            placeholder: "请选择最小值字段",
            style: { "width": "calc(50% - 20px)" }
          }, null, 8, ["modelValue"]),
          _cache[14] || (_cache[14] = createElementVNode("span", { style: { "margin": "0 5px" } }, "-", -1)),
          createVNode(ValueSelector, {
            modelValue: unref(rangeMax),
            "onUpdate:modelValue": _cache[2] || (_cache[2] = ($event) => isRef(rangeMax) ? rangeMax.value = $event : null),
            placeholder: "请选择最大值字段",
            style: { "width": "50%" },
            size: __props.size
          }, null, 8, ["modelValue", "size"])
        ])) : (openBlock(), createElementBlock("div", _hoisted_4, [
          unref(isRange) && __props.fieldType === "number" ? (openBlock(), createElementBlock("div", _hoisted_5, [
            createVNode(_component_el_input, {
              modelValue: unref(rangeMin),
              "onUpdate:modelValue": _cache[3] || (_cache[3] = ($event) => isRef(rangeMin) ? rangeMin.value = $event : null),
              modelModifiers: { number: true },
              placeholder: "最小值",
              type: "number",
              size: __props.size,
              style: { "width": "calc(50% - 20px)" }
            }, null, 8, ["modelValue", "size"]),
            _cache[15] || (_cache[15] = createElementVNode("span", { style: { "margin": "0 5px" } }, "-", -1)),
            createVNode(_component_el_input, {
              modelValue: unref(rangeMax),
              "onUpdate:modelValue": _cache[4] || (_cache[4] = ($event) => isRef(rangeMax) ? rangeMax.value = $event : null),
              modelModifiers: { number: true },
              placeholder: "最大值",
              type: "number",
              size: __props.size,
              style: { "width": "50%" }
            }, null, 8, ["modelValue", "size"])
          ])) : unref(isRange) && (__props.fieldType === "date" || __props.fieldType === "datetime") ? (openBlock(), createElementBlock("div", _hoisted_6, [
            createVNode(_component_el_date_picker, {
              modelValue: unref(rangeMin),
              "onUpdate:modelValue": _cache[5] || (_cache[5] = ($event) => isRef(rangeMin) ? rangeMin.value = $event : null),
              type: __props.fieldType === "datetime" ? "datetime" : "date",
              placeholder: "开始日期",
              size: __props.size,
              style: { "width": "calc(50% - 20px)" },
              "value-format": "YYYYMMDDHHmmss"
            }, null, 8, ["modelValue", "type", "size"]),
            _cache[16] || (_cache[16] = createElementVNode("span", { style: { "margin": "0 5px" } }, "-", -1)),
            createVNode(_component_el_date_picker, {
              modelValue: unref(rangeMax),
              "onUpdate:modelValue": _cache[6] || (_cache[6] = ($event) => isRef(rangeMax) ? rangeMax.value = $event : null),
              type: __props.fieldType === "datetime" ? "datetime" : "date",
              placeholder: "结束日期",
              size: __props.size,
              style: { "width": "50%" },
              "value-format": "YYYYMMDDHHmmss"
            }, null, 8, ["modelValue", "type", "size"])
          ])) : unref(isTagInput) ? (openBlock(), createBlock(_component_el_input_tag, {
            key: 2,
            modelValue: unref(data),
            "onUpdate:modelValue": _cache[7] || (_cache[7] = ($event) => isRef(data) ? data.value = $event : null),
            trigger: "Space",
            placeholder: "输入后空格或失去焦点添加",
            style: { "width": "100%" },
            size: __props.size
          }, null, 8, ["modelValue", "size"])) : __props.fieldType === "select" ? (openBlock(), createBlock(_component_el_select, {
            key: 3,
            modelValue: unref(data),
            "onUpdate:modelValue": _cache[8] || (_cache[8] = ($event) => isRef(data) ? data.value = $event : null),
            multiple: unref(isMultiple),
            filterable: "",
            placeholder: "请选择",
            style: { "width": "100%" },
            size: __props.size
          }, {
            default: withCtx(() => [
              (openBlock(true), createElementBlock(Fragment, null, renderList(unref(selectOptions), (item) => {
                return openBlock(), createBlock(_component_el_option, {
                  key: item.value,
                  label: item.label,
                  value: item.value
                }, null, 8, ["label", "value"]);
              }), 128))
            ]),
            _: 1
          }, 8, ["modelValue", "multiple", "size"])) : __props.fieldType === "number" ? (openBlock(), createBlock(_component_el_input, {
            key: 4,
            modelValue: unref(data),
            "onUpdate:modelValue": _cache[9] || (_cache[9] = ($event) => isRef(data) ? data.value = $event : null),
            modelModifiers: { number: true },
            type: "number",
            placeholder: "请输入数值",
            size: __props.size,
            style: { "width": "100%" }
          }, null, 8, ["modelValue", "size"])) : __props.fieldType === "date" ? (openBlock(), createBlock(_component_el_date_picker, {
            key: 5,
            modelValue: unref(data),
            "onUpdate:modelValue": _cache[10] || (_cache[10] = ($event) => isRef(data) ? data.value = $event : null),
            type: "date",
            placeholder: "请选择日期",
            size: __props.size,
            style: { "width": "100%" },
            "value-format": "YYYYMMDD"
          }, null, 8, ["modelValue", "size"])) : __props.fieldType === "datetime" ? (openBlock(), createBlock(_component_el_date_picker, {
            key: 6,
            modelValue: unref(data),
            "onUpdate:modelValue": _cache[11] || (_cache[11] = ($event) => isRef(data) ? data.value = $event : null),
            type: "datetime",
            placeholder: "请选择日期时间",
            size: __props.size,
            style: { "width": "100%" },
            "value-format": "YYYYMMDDHHmmss"
          }, null, 8, ["modelValue", "size"])) : __props.fieldType === "boolean" ? (openBlock(), createBlock(_component_el_select, {
            key: 7,
            modelValue: unref(data),
            "onUpdate:modelValue": _cache[12] || (_cache[12] = ($event) => isRef(data) ? data.value = $event : null),
            placeholder: "请选择",
            style: { "width": "100%" },
            size: __props.size
          }, {
            default: withCtx(() => [
              createVNode(_component_el_option, {
                label: "是",
                value: true
              }),
              createVNode(_component_el_option, {
                label: "否",
                value: false
              })
            ]),
            _: 1
          }, 8, ["modelValue", "size"])) : (openBlock(), createBlock(_component_el_input, {
            key: 8,
            modelValue: unref(data),
            "onUpdate:modelValue": _cache[13] || (_cache[13] = ($event) => isRef(data) ? data.value = $event : null),
            placeholder: "请输入",
            size: __props.size,
            style: { "width": "100%" }
          }, null, 8, ["modelValue", "size"]))
        ]))
      ]));
    };
  }
};
const ValueInput = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-eddee3ea"]]);
export {
  ValueInput as default
};
