"use strict";
const uni_modules_aureCountryPicker_components_aureCountryPicker_countryData = require("./country-data.js");
const common_vendor = require("../../../../common/vendor.js");
const _sfc_main = {
  name: "aure-country-picker",
  props: {
    /**
     * v-model 当前选中项（对象），结构同 country-data.js
     */
    modelValue: [String, Number],
    /**
     * 国家 Code 默认值（如 'US'、'CA'）
     * 用于 dial_code 重复时辅助定位国家（如 1 可对应多个）
     */
    defaultCountryCode: {
      type: String,
      default: ""
    },
    /**
     * 取消按钮文案
     * */
    cancelText: {
      type: String,
      default: "取消"
    },
    /**
     * 搜索框占位符
     * */
    searchPlaceholder: {
      type: String,
      default: "搜索国家或地区"
    },
    /**
     * 弹窗标题
     */
    title: { type: String, default: "选择国家/地区" },
    /**
     * bottom/top弹窗高度（如 '80vh'、'480rpx'、'80%'）
     */
    height: { type: String, default: "80vh" },
    /**
     * left/right弹窗宽度如 '80vw'、'480rpx'、'80%'）
     */
    width: { type: String, default: "80vw" },
    /**
     * 弹窗动画时长（ms）
     */
    duration: { type: Number, default: 300 },
    /**
     * 是否允许点击遮罩关闭弹窗
     */
    maskClosable: { type: Boolean, default: true },
    /**
     * 是否圆角
     */
    round: { type: Boolean, default: true },
    /**
     * 圆角半径
     */
    radius: { type: String, default: "24rpx" },
    /**
     * 弹窗弹出方向
     */
    position: {
      type: String,
      default: "bottom",
      // bottom | top | left | right
      validator: (v) => ["bottom", "top", "left", "right"].includes(v)
    },
    /**
     * （高级）自定义数据源，结构与 country-data.js 相同
     */
    countryList: { type: Array, default: () => uni_modules_aureCountryPicker_components_aureCountryPicker_countryData.countryListRaw }
  },
  emits: ["update:modelValue", "change"],
  data() {
    return {
      currentSelectInfo: {
        name: "",
        code: "",
        dial_code: "",
        localName: ""
      },
      mounted: false,
      showPopup: false,
      search: "",
      realSelected: null
    };
  },
  computed: {
    // 实际展示的国家数据
    realCountryList() {
      return this.countryList && this.countryList.length > 0 ? this.countryList : uni_modules_aureCountryPicker_components_aureCountryPicker_countryData.countryListRaw;
    },
    filteredList() {
      const key = this.search.trim().toLowerCase();
      if (!key)
        return this.realCountryList;
      return this.realCountryList.filter((item) => {
        const name = (item.name || "").toLowerCase();
        const localName = (item.localName || "").toLowerCase();
        const code = (item.code || "").toLowerCase();
        const dial_code = item.dial_code || "";
        return name.includes(key) || localName.includes(key) || code.includes(key) || dial_code && dial_code.startsWith(key);
      });
    },
    panelStyle() {
      let style = {
        transition: `transform ${this.duration}ms cubic-bezier(.4,0,.2,1)`
      };
      if (this.position === "bottom" || this.position === "top") {
        style.width = "100vw";
        style.height = this.height;
      } else {
        style.width = this.width || "80vw";
        style.height = "100vh";
      }
      if (this.round) {
        if (this.position === "bottom")
          style.borderRadius = `${this.radius} ${this.radius} 0 0`;
        else if (this.position === "top")
          style.borderRadius = `0 0 ${this.radius} ${this.radius}`;
        else if (this.position === "left")
          style.borderRadius = `0 ${this.radius} ${this.radius} 0`;
        else if (this.position === "right")
          style.borderRadius = `${this.radius} 0 0 ${this.radius}`;
      } else {
        style.borderRadius = "0";
      }
      return style;
    }
  },
  mounted() {
    if (this.modelValue) {
      const matchedList = this.realCountryList.filter(
        (i) => String(i.dial_code) === String(this.modelValue)
      );
      if (matchedList.length === 1) {
        this.currentSelectInfo = matchedList[0];
      } else if (matchedList.length > 1 && this.defaultCountryCode) {
        const precise = matchedList.find((i) => i.code === this.defaultCountryCode.toUpperCase());
        if (precise) {
          this.currentSelectInfo = precise;
        } else {
          this.currentSelectInfo = matchedList[0];
        }
      } else if (matchedList.length > 0) {
        this.currentSelectInfo = matchedList[0];
      }
    }
  },
  methods: {
    open() {
      this.mounted = true;
      this.$nextTick(() => {
        setTimeout(() => {
          this.showPopup = true;
        }, 20);
      });
    },
    close() {
      this.showPopup = false;
    },
    onMaskClick() {
      if (!this.maskClosable)
        return;
      this.close();
    },
    onAnimEnd() {
      if (!this.showPopup) {
        this.mounted = false;
        this.search = "";
      }
    },
    select(item) {
      this.$emit("update:modelValue", item.dial_code);
      this.$emit("change", item);
      this.onSelected(item);
      this.close();
    },
    onSelected(selectItem) {
      this.currentSelectInfo = this.realCountryList.find(
        (item) => String(item.code) === String(selectItem.code) && String(item.dial_code) === String(selectItem.dial_code)
      );
    },
    getFlagSrc(code) {
      return `/uni_modules/aure-country-picker/static/flags/${code.toLowerCase()}.png`;
    },
    clearSearch() {
      this.search = "";
    }
  }
};
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  return common_vendor.e({
    a: $data.currentSelectInfo.code
  }, $data.currentSelectInfo.code ? {
    b: $options.getFlagSrc($data.currentSelectInfo.code)
  } : {}, {
    c: common_vendor.t($data.currentSelectInfo.dial_code || "--"),
    d: common_vendor.o((...args) => $options.open && $options.open(...args)),
    e: $data.mounted
  }, $data.mounted ? {
    f: common_vendor.t($props.title),
    g: common_vendor.t($props.cancelText),
    h: common_vendor.o((...args) => $options.close && $options.close(...args)),
    i: $props.searchPlaceholder,
    j: common_vendor.o((...args) => $options.clearSearch && $options.clearSearch(...args)),
    k: $data.search,
    l: common_vendor.o(($event) => $data.search = $event.detail.value),
    m: common_vendor.f($options.filteredList, (item, k0, i0) => {
      return {
        a: $options.getFlagSrc(item.code),
        b: common_vendor.t(item.localName),
        c: common_vendor.t(item.dial_code),
        d: item.code + item.dial_code,
        e: common_vendor.o(($event) => $options.select(item), item.code + item.dial_code)
      };
    }),
    n: common_vendor.n(`popup-panel--${$props.position}`),
    o: common_vendor.n({
      "popup-panel--show": $data.showPopup
    }),
    p: common_vendor.s($options.panelStyle),
    q: common_vendor.o((...args) => $options.onAnimEnd && $options.onAnimEnd(...args)),
    r: common_vendor.o(() => {
    }),
    s: common_vendor.o(() => {
    }),
    t: common_vendor.o((...args) => $options.onMaskClick && $options.onMaskClick(...args))
  } : {});
}
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-3a3a894d"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/aure-country-picker/components/aure-country-picker/aure-country-picker.js.map
