import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import { reactive, computed } from 'vue';
import { isArray } from 'lodash-es';

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; }
function useGenUid() {
  let seed = 0;
  const now = Date.now();
  function genUid() {
    return `select_group_${now}_${seed++}`;
  }
  return {
    genUid
  };
}
var useOptions = _ref => {
  let {
    props
  } = _ref;
  const childOptions = reactive([]);
  const {
    genUid
  } = useGenUid();
  const addOption = (option, groupOption) => {
    if (groupOption) {
      if (!groupOption.children.includes(option)) {
        groupOption.children.push(option);
      }
    } else {
      if (!childOptions.includes(option)) {
        childOptions.push(option);
      }
    }
  };
  const removeOption = (id, groupOption) => {
    if (groupOption) {
      const colIndex = groupOption.children.findIndex(item => item.id === id);
      if (colIndex !== -1) {
        groupOption.children.splice(colIndex, 1);
      }
    } else {
      const colIndex = childOptions.findIndex(item => item.id === id);
      if (colIndex !== -1) {
        childOptions.splice(colIndex, 1);
      }
    }
  };
  const baseOptions = computed(() => {
    const getOption = _ref2 => {
      let {
        option,
        groupOption,
        isChildOption
      } = _ref2;
      // Option 组件没有配置 value 默认为 undefined & Foption 无需处理 valueField 和 labelField
      const currentOption = _objectSpread(_objectSpread({}, option), {}, {
        value: isChildOption ? option.value : option[props.valueField],
        label: isChildOption ? option.label : option[props.labelField],
        // 当分组禁用时，子选项都禁用
        disabled: (groupOption === null || groupOption === void 0 ? void 0 : groupOption.disabled) || option.disabled,
        __level: ((groupOption === null || groupOption === void 0 ? void 0 : groupOption.__level) || 0) + 1
      });
      if (isArray(currentOption.children)) {
        currentOption.__isGroup = true;
        // 虚拟滚动，需要指定 value
        currentOption.value = currentOption.value || genUid();
        const children = currentOption.children.map(subOption => {
          return getOption({
            option: subOption,
            groupOption: currentOption,
            isChildOption
          });
        });
        currentOption.children = children;
      } else {
        currentOption.__isGroup = false;
      }
      return currentOption;
    };
    const currentChildOptions = [...childOptions].reduce((acc, option) => {
      return acc.concat(getOption({
        option,
        isChildOption: true
      }));
    }, []);
    const currentPropsOptions = [...(props.options || [])].reduce((acc, option) => {
      return acc.concat(getOption({
        option,
        isChildOption: false
      }));
    }, []);
    return [...currentChildOptions, ...currentPropsOptions];
  });
  const flatBaseOptions = computed(() => {
    const getFlatOptions = options => {
      let flatOptions = [];
      options.forEach(option => {
        if (option.__isGroup) {
          flatOptions = flatOptions.concat([option].concat(getFlatOptions(option.children)));
        } else {
          flatOptions.push(option);
        }
      });
      return flatOptions;
    };
    return baseOptions.value.reduce((acc, option) => {
      return acc.concat(getFlatOptions([option]));
    }, []);
  });
  return {
    addOption,
    removeOption,
    flatBaseOptions
  };
};

export { useOptions as default };
