import { defineComponent, computed, provide, createVNode, ref } from 'vue';
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { DESCRIPTIONS_PROVIDE_KEY, DESCRIPTIONS_PREFIX_CLASS } from './constants';
import { descriptionsProps } from './props';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
const prefixCls = DESCRIPTIONS_PREFIX_CLASS;
const useItems = () => {
  const instances = ref([]);
  const add = instance => {
    const nextInstances = [...instances.value];
    nextInstances.push(instance);
    nextInstances.sort((_ref, _ref2) => {
      let {
        index: indexA
      } = _ref;
      let {
        index: indexB
      } = _ref2;
      return indexA - indexB;
    });
    instances.value = nextInstances;
  };
  const remove = id => {
    const index = instances.value.findIndex(i => i.id === id);
    if (index === -1) {
      return;
    }
    const nextInstances = [...instances.value];
    nextInstances.splice(index, 1);
    instances.value = nextInstances.map((instance, index) => _objectSpread(_objectSpread({}, instance), {}, {
      index
    }));
  };
  return {
    items: instances,
    addItem: add,
    removeItem: remove
  };
};
var descriptions = defineComponent({
  name: 'FDescriptions',
  props: descriptionsProps,
  setup(props, _ref3) {
    let {
      slots
    } = _ref3;
    const style = computed(() => ({
      'grid-template-columns': `repeat(${props.column}, 1fr)`
    }));
    const renderHeader = () => {
      if (slots.header) {
        return slots.header();
      }
      if (props.title) {
        return createVNode("div", {
          "class": `${prefixCls}-header`
        }, [props.title]);
      }
      return null;
    };
    const {
      items,
      addItem,
      removeItem
    } = useItems();
    provide(DESCRIPTIONS_PROVIDE_KEY, {
      parentProps: computed(() => {
        return {
          column: props.column,
          contentStyle: props.contentStyle,
          labelAlign: props.labelAlign,
          labelPlacement: props.labelPlacement,
          labelStyle: props.labelStyle,
          separator: props.separator,
          bordered: props.bordered
        };
      }),
      items,
      addItem,
      removeItem
    });
    return () => {
      var _slots$default;
      return createVNode("div", {
        "class": [`${prefixCls}`, props.size && `${prefixCls}-size-${props.size}`]
      }, [renderHeader(), createVNode("div", {
        "class": [`${prefixCls}-body`, props.bordered && 'is-bordered'],
        "style": style.value
      }, [(_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)])]);
    };
  }
});

export { descriptions as default };
