"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const vue = require("vue");
const comp = require("../../ui/src/comp.js");
const XEUtils = require("xe-utils");
require("../../ui/index.js");
const dom = require("../../ui/src/dom.js");
const config = require("../../../../@vxe-ui/core/es/src/config.js");
const useFns = require("../../../../@vxe-ui/core/es/src/useFns.js");
const event = require("../../../../@vxe-ui/core/es/src/event.js");
const index_esm = require("../../../../@vxe-ui/core/es/index.esm.js");
function createReactData() {
  return {
    groupName: XEUtils.uniqueId("xe_group_"),
    selectStyle: {
      display: "",
      left: "",
      width: ""
    }
  };
}
const VxeSegmentedComponent = comp.defineVxeComponent({
  name: "VxeSegmented",
  props: {
    modelValue: [String, Number],
    name: [String, Number],
    disabled: Boolean,
    options: Array,
    type: {
      type: String,
      default: () => config.getConfig().segmented.type
    },
    status: String,
    width: [String, Number],
    height: [String, Number],
    optionConfig: Object,
    size: {
      type: String,
      default: () => config.getConfig().segmented.size || config.getConfig().size
    }
  },
  emits: [
    "update:modelValue",
    "change"
  ],
  setup(props, context) {
    const { slots, emit } = context;
    const xID = XEUtils.uniqueId();
    const { computeSize } = useFns.useSize(props);
    const refElem = vue.ref();
    const refWrapperElem = vue.ref();
    const refSelectedElem = vue.ref();
    const reactData = vue.reactive(createReactData());
    const refMaps = {
      refElem
    };
    const computeItemType = vue.computed(() => {
      const { type } = props;
      if (type === "round") {
        return type;
      }
      if (type === "inside") {
        return type;
      }
      return "default";
    });
    const computeWrapperStyle = vue.computed(() => {
      const { width, height } = props;
      const stys = {};
      if (width) {
        stys.width = dom.toCssUnit(width);
      }
      if (height) {
        stys.height = dom.toCssUnit(height);
      }
      return stys;
    });
    const computeOptionOpts = vue.computed(() => {
      return Object.assign({}, config.getConfig().segmented.optionConfig, props.optionConfig);
    });
    const computeItemList = vue.computed(() => {
      return props.options || [];
    });
    const computeSelectIndex = vue.computed(() => {
      const { modelValue } = props;
      const itemList = computeItemList.value;
      return XEUtils.findIndexOf(itemList, (item) => item.value === modelValue);
    });
    const comIsFullWidth = vue.computed(() => {
      const wrapperStyle = computeWrapperStyle.value;
      return wrapperStyle.width === "100%";
    });
    const computeMaps = {};
    const $xeSegmented = {
      xID,
      props,
      context,
      reactData,
      getRefMaps: () => refMaps,
      getComputeMaps: () => computeMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, event.createEvent(evnt, { $segmented: $xeSegmented }, params));
    };
    const emitModel = (value) => {
      emit("update:modelValue", value);
    };
    const changeEvent = (evnt, item) => {
      const value = item.value;
      emitModel(value);
      dispatchEvent("change", { value }, evnt);
      updateStyle();
    };
    const updateStyle = () => {
      vue.nextTick(() => {
        const selectIndex = computeSelectIndex.value;
        const wrapperElem = refWrapperElem.value;
        const atStyle = {
          display: "",
          left: "",
          width: ""
        };
        if (wrapperElem) {
          const itemEl = wrapperElem.children[selectIndex];
          if (itemEl) {
            atStyle.display = "block";
            atStyle.left = dom.toCssUnit(itemEl.offsetLeft);
            atStyle.width = dom.toCssUnit(itemEl.clientWidth);
          }
        }
        reactData.selectStyle = atStyle;
      });
    };
    const tagMethods = {
      dispatchEvent
    };
    const tagPrivateMethods = {};
    Object.assign($xeSegmented, tagMethods, tagPrivateMethods);
    const renderItems = () => {
      const { modelValue, name } = props;
      const { groupName } = reactData;
      const optionOpts = computeOptionOpts.value;
      const itemList = computeItemList.value;
      const isFullWidth = comIsFullWidth.value;
      const fullItemWidth = isFullWidth ? Math.max(0, 100 / itemList.length) : 0;
      const defaultSlot = slots.default;
      const itemVNs = [];
      itemList.forEach((item, i) => {
        const { icon, width } = item;
        const itemWidth = width || optionOpts.width;
        itemVNs.push(vue.h("label", {
          key: i,
          class: ["vxe-segmented--item", {
            "is--checked": modelValue === item.value
          }],
          style: isFullWidth || itemWidth ? {
            width: itemWidth ? dom.toCssUnit(itemWidth) : fullItemWidth ? `${fullItemWidth}%` : ""
          } : void 0
        }, [
          vue.h("input", {
            class: "vxe-segmented--input",
            type: "radio",
            name: name || groupName,
            onChange(evnt) {
              changeEvent(evnt, item);
            }
          }),
          vue.h("div", {
            class: "vxe-segmented--content"
          }, [
            icon ? vue.h("div", {
              class: "vxe-segmented--icon"
            }, [
              vue.h("i", {
                class: icon
              })
            ]) : index_esm.renderEmptyElement(),
            vue.h("div", {
              class: "vxe-segmented--label"
            }, defaultSlot ? defaultSlot({ option: item }) : XEUtils.eqNull(item.label) ? "" : `${item.label}`)
          ])
        ]));
      });
      return itemVNs;
    };
    const renderVN = () => {
      const { status } = props;
      const { selectStyle } = reactData;
      const itemType = computeItemType.value;
      const wrapperStyle = computeWrapperStyle.value;
      const vSize = computeSize.value;
      return vue.h("div", {
        ref: refElem,
        class: ["vxe-segmented", `type--${itemType}`, {
          [`size--${vSize}`]: vSize,
          [`theme--${status}`]: status
        }]
      }, [
        vue.h("div", {
          class: "vxe-segmented--group",
          style: wrapperStyle
        }, [
          vue.h("div", {
            ref: refSelectedElem,
            class: "vxe-segmented--selected",
            style: selectStyle
          }),
          vue.h("div", {
            ref: refWrapperElem,
            class: "vxe-segmented--inner"
          }, renderItems())
        ])
      ]);
    };
    vue.watch(computeSelectIndex, () => {
      updateStyle();
    });
    vue.onMounted(() => {
      updateStyle();
    });
    vue.onBeforeUnmount(() => {
      XEUtils.assign(reactData, createReactData());
    });
    $xeSegmented.renderVN = renderVN;
    return $xeSegmented;
  },
  render() {
    return this.renderVN();
  }
});
exports.default = VxeSegmentedComponent;
