"use strict";
const common_vendor = require("../../common/vendor.js");
if (!Math) {
  Icon();
}
const Icon = () => "../icon/index.js";
const _sfc_main = {
  __name: "index",
  props: {
    // 选项数组
    options: {
      type: Array,
      default: () => []
    },
    // 字段映射
    mapping: {
      type: Object,
      default: () => ({ text: "n", value: "v" })
    },
    // 默认值
    modelValue: {
      type: [String, Number],
      default: ""
    },
    // 占位符
    placeholder: {
      type: String,
      default: "请选择"
    },
    // 校验配置
    validation: {
      type: Object,
      default: () => ({ enable: false, message: "" })
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    // 是否支持搜索
    searchable: {
      type: Boolean,
      default: false
    },
    // 是否支持清空
    clearable: {
      type: Boolean,
      default: false
    },
    // 加载状态
    loading: {
      type: Boolean,
      default: false
    },
    // 宽度设置
    width: {
      type: String,
      default: "100%"
    }
  },
  emits: ["update:modelValue", "change", "search", "clear"],
  setup(__props, { expose: __expose, emit: __emit }) {
    common_vendor.useCssVars((_ctx) => ({
      "5e889c68": __props.width
    }));
    const props = __props;
    const emit = __emit;
    const isOpen = common_vendor.ref(false);
    const selectedValue = common_vendor.ref(props.modelValue);
    common_vendor.ref("");
    const optionsList = common_vendor.computed(() => props.options || []);
    const displayText = common_vendor.computed(() => {
      if (!selectedValue.value)
        return props.placeholder;
      const selectedOption = optionsList.value.find(
        (option) => option[props.mapping.value] === selectedValue.value
      );
      return selectedOption ? selectedOption[props.mapping.text] : props.placeholder;
    });
    const toggleDropdown = () => {
      if (props.disabled)
        return;
      if (!isOpen.value) {
        common_vendor.index.$emit("closeAllSelects");
      }
      isOpen.value = !isOpen.value;
    };
    const selectOption = (option) => {
      const value = option[props.mapping.value];
      selectedValue.value = value;
      isOpen.value = false;
      emit("update:modelValue", value);
      emit("change", value, option);
    };
    const closeDropdown = () => {
      isOpen.value = false;
    };
    const setValue = (value) => {
      if (!value) {
        selectedValue.value = "";
        emit("update:modelValue", "");
        return true;
      }
      const option = optionsList.value.find(
        (opt) => opt[props.mapping.value] === value
      );
      if (option) {
        selectedValue.value = value;
        emit("update:modelValue", value);
        emit("change", value, option);
        return true;
      }
      return false;
    };
    common_vendor.watch(() => props.modelValue, (newValue) => {
      selectedValue.value = newValue;
    });
    const handleGlobalClose = () => {
      closeDropdown();
    };
    common_vendor.onMounted(() => {
      common_vendor.index.$on("closeAllSelects", handleGlobalClose);
      common_vendor.index.$on("pageClick", closeDropdown);
    });
    common_vendor.onUnmounted(() => {
      common_vendor.index.$off("closeAllSelects", handleGlobalClose);
      common_vendor.index.$off("pageClick", closeDropdown);
    });
    __expose({
      setValue,
      getValue: () => selectedValue.value,
      getSelectedOption: () => {
        return optionsList.value.find(
          (option) => option[props.mapping.value] === selectedValue.value
        );
      }
    });
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.t(displayText.value),
        b: !selectedValue.value ? 1 : "",
        c: common_vendor.p({
          name: "icon-xiangxiajiantou",
          size: "32",
          color: __props.disabled ? "#ccc" : "#999"
        }),
        d: isOpen.value ? 1 : "",
        e: __props.disabled ? 1 : "",
        f: __props.loading
      }, __props.loading ? {} : optionsList.value.length === 0 ? {} : {
        h: common_vendor.f(optionsList.value, (option, index, i0) => {
          return {
            a: common_vendor.t(option[props.mapping.text]),
            b: index,
            c: option[props.mapping.value] === selectedValue.value ? 1 : "",
            d: common_vendor.o(($event) => selectOption(option), index)
          };
        })
      }, {
        g: optionsList.value.length === 0,
        i: isOpen.value,
        j: isOpen.value ? 1 : "",
        k: common_vendor.o(toggleDropdown),
        l: common_vendor.s(_ctx.__cssVars()),
        m: common_vendor.gei(_ctx, "")
      });
    };
  }
};
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-ac2d57b0"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../.sourcemap/mp-weixin/components/SsSelect/index.js.map
