'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var index = require('../style/index.js');
var classNames = require('../../_util/classNames.js');
var useConfigInject = require('../../config-provider/hooks/useConfigInject.js');
require('../../_util/props-util/index.js');
var type = require('../../_util/type.js');
var MotionThumb = require('./MotionThumb.js');
var initDefaultProps = require('../../_util/props-util/initDefaultProps.js');

function normalizeOptions(options) {
  return options.map((option) => {
    if (typeof option === "object" && option !== null)
      return option;
    return {
      label: option == null ? void 0 : option.toString(),
      title: option == null ? void 0 : option.toString(),
      value: option
    };
  });
}
const segmentedProps = () => {
  return {
    "prefixCls": String,
    "options": type.arrayType(),
    "block": type.booleanType(),
    "disabled": type.booleanType(),
    "size": type.stringType(),
    "value": {
      ...type.someType([String, Number]),
      required: true
    },
    "motionName": String,
    "onChange": type.functionType(),
    "onUpdate:value": type.functionType()
  };
};
const SegmentedOption = (props, {
  slots,
  emit
}) => {
  const {
    value,
    disabled,
    payload,
    title,
    prefixCls,
    label = slots.label,
    checked,
    className
  } = props;
  const handleChange = (event) => {
    if (disabled)
      return;
    emit("change", event, value);
  };
  return vue.createVNode("label", {
    "class": classNames.default({
      [`${prefixCls}-item-disabled`]: disabled
    }, className)
  }, [vue.createVNode("input", {
    "class": `${prefixCls}-item-input`,
    "type": "radio",
    "disabled": disabled,
    "checked": checked,
    "onChange": handleChange
  }, null), vue.createVNode("div", {
    "class": `${prefixCls}-item-label`,
    "title": typeof title === "string" ? title : ""
  }, [typeof label === "function" ? label({
    value,
    disabled,
    payload,
    title
  }) : label != null ? label : value])]);
};
SegmentedOption.inheritAttrs = false;
var Segmented = /* @__PURE__ */ vue.defineComponent({
  name: "ASegmented",
  inheritAttrs: false,
  props: initDefaultProps.default(segmentedProps(), {
    options: [],
    motionName: "thumb-motion"
  }),
  slots: Object,
  setup(props, {
    emit,
    slots,
    attrs
  }) {
    const {
      prefixCls,
      direction,
      size
    } = useConfigInject.default("segmented", props);
    const [wrapSSR, hashId] = index.default(prefixCls);
    const rootRef = vue.shallowRef();
    const thumbShow = vue.shallowRef(false);
    const segmentedOptions = vue.computed(() => normalizeOptions(props.options));
    const handleChange = (_event, val) => {
      if (props.disabled)
        return;
      emit("update:value", val);
      emit("change", val);
    };
    return () => {
      const pre = prefixCls.value;
      return wrapSSR(vue.createVNode("div", vue.mergeProps(attrs, {
        "class": classNames.default(pre, {
          [hashId.value]: true,
          [`${pre}-block`]: props.block,
          [`${pre}-disabled`]: props.disabled,
          [`${pre}-lg`]: size.value === "large",
          [`${pre}-sm`]: size.value === "small",
          [`${pre}-rtl`]: direction.value === "rtl"
        }, attrs.class),
        "ref": rootRef
      }), [vue.createVNode("div", {
        "class": `${pre}-group`
      }, [vue.createVNode(MotionThumb.default, {
        "containerRef": rootRef,
        "prefixCls": pre,
        "value": props.value,
        "motionName": `${pre}-${props.motionName}`,
        "direction": direction.value,
        "getValueIndex": (val) => segmentedOptions.value.findIndex((n) => n.value === val),
        "onMotionStart": () => {
          thumbShow.value = true;
        },
        "onMotionEnd": () => {
          thumbShow.value = false;
        }
      }, null), segmentedOptions.value.map((segmentedOption) => vue.createVNode(SegmentedOption, vue.mergeProps({
        "key": segmentedOption.value,
        "prefixCls": pre,
        "checked": segmentedOption.value === props.value,
        "onChange": handleChange
      }, segmentedOption, {
        "className": classNames.default(segmentedOption.className, `${pre}-item`, {
          [`${pre}-item-selected`]: segmentedOption.value === props.value && !thumbShow.value
        }),
        "disabled": !!props.disabled || !!segmentedOption.disabled
      }), slots))])]));
    };
  }
});

exports.default = Segmented;
exports.segmentedProps = segmentedProps;
//# sourceMappingURL=segmented.js.map
