import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { createVNode as _createVNode } from "vue";
import _extends from "@babel/runtime/helpers/esm/extends";
import { flattenChildren } from '../_util/props-util';
import { computed, defineComponent, toRef } from 'vue';
import { withInstall } from '../_util/type';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import useStyle from './style';
import '../config-provider/SizeContext';
import useSize from '../config-provider/hooks/useSize';
export const dividerProps = () => ({
  prefixCls: String,
  type: {
    type: String,
    default: 'horizontal'
  },
  dashed: {
    type: Boolean,
    default: false
  },
  orientation: {
    type: String,
    default: 'center'
  },
  plain: {
    type: Boolean,
    default: false
  },
  orientationMargin: [String, Number],
  variant: {
    type: String
  },
  size: {
    type: String
  }
});
const sizeClassNameMap = {
  small: 'sm',
  middle: 'md'
};
const Divider = defineComponent({
  name: 'ADivider',
  inheritAttrs: false,
  compatConfig: {
    MODE: 3
  },
  props: dividerProps(),
  setup(props, {
    slots,
    attrs
  }) {
    const {
      prefixCls: prefixClsRef,
      direction
    } = useConfigInject('divider', props);
    const [wrapSSR, hashId] = useStyle(prefixClsRef);
    const mergedOrientation = computed(() => {
      const orientation = props.orientation;
      if (orientation === 'left') {
        return direction.value === 'rtl' ? 'end' : 'start';
      }
      if (orientation === 'right') {
        return direction.value === 'rtl' ? 'start' : 'end';
      }
      return orientation;
    });
    const sizeFullName = useSize(toRef(props, 'size'));
    const sizeCls = computed(() => sizeClassNameMap[sizeFullName.value]);
    const hasMarginStart = computed(() => mergedOrientation.value === 'start' && props.orientationMargin != null);
    const hasMarginEnd = computed(() => mergedOrientation.value === 'end' && props.orientationMargin != null);
    const classString = computed(() => {
      const {
        type,
        dashed,
        plain,
        variant
      } = props;
      const prefixCls = prefixClsRef.value;
      return {
        [prefixCls]: true,
        [hashId.value]: !!hashId.value,
        [`${prefixCls}-${type}`]: true,
        [`${prefixCls}-dashed`]: !!dashed,
        [`${prefixCls}-${variant}`]: variant !== 'solid',
        [`${prefixCls}-plain`]: !!plain,
        [`${prefixCls}-rtl`]: direction.value === 'rtl',
        [`${prefixCls}-no-default-orientation-margin-left`]: hasMarginStart.value,
        [`${prefixCls}-no-default-orientation-margin-right`]: hasMarginEnd.value,
        [`${prefixCls}-${sizeCls.value}`]: !!sizeCls.value
      };
    });
    const innerStyle = computed(() => {
      const marginValue = typeof props.orientationMargin === 'number' ? `${props.orientationMargin}px` : props.orientationMargin;
      return _extends(_extends({}, hasMarginStart.value && {
        marginInlineStart: marginValue
      }), hasMarginEnd.value && {
        marginInlineEnd: marginValue
      });
    });
    return () => {
      var _a;
      const children = flattenChildren((_a = slots.default) === null || _a === void 0 ? void 0 : _a.call(slots));
      return wrapSSR(_createVNode("div", _objectSpread(_objectSpread({}, attrs), {}, {
        "class": [classString.value, children.length ? `${prefixClsRef.value}-with-text ${prefixClsRef.value}-with-text-${mergedOrientation.value}` : '', attrs.class],
        "role": "separator"
      }), [children.length && props.type !== 'vertical' ? _createVNode("span", {
        "class": `${prefixClsRef.value}-inner-text`,
        "style": innerStyle.value
      }, [children]) : null]));
    };
  }
});
export default withInstall(Divider);