'use strict';

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

var vue = require('vue');
var usePropsAlias = require('./use-props-alias.js');

const useCheck = (props, panelState) => {
  const propsAlias = usePropsAlias.usePropsAlias(props);
  const getLastNodeSum = (data, sum) => {
    return data.reduce(($sum, transferDataItem) => {
      if (!transferDataItem[propsAlias.value.children] || !transferDataItem[propsAlias.value.children].length) {
        $sum = $sum + 1;
      } else {
        $sum = getLastNodeSum(transferDataItem[propsAlias.value.children], $sum);
      }
      return $sum;
    }, sum);
  };
  const getCheckableData = (data, checkedData) => {
    data.forEach((transferDataItem) => {
      if (!transferDataItem[propsAlias.value.children] || !transferDataItem[propsAlias.value.children].length) {
        if (!transferDataItem[propsAlias.value.disabled]) {
          checkedData.push(transferDataItem);
        }
      } else {
        getCheckableData(transferDataItem[propsAlias.value.children], checkedData);
      }
    });
    return checkedData;
  };
  const checkableData = vue.computed(() => getCheckableData(props.data, []));
  const checkedSummary = vue.computed(() => {
    const checkedLength = panelState.checked.length;
    const dataLength = getLastNodeSum(props.data, 0);
    return `${checkedLength}/${dataLength}`;
  });
  const isIndeterminate = vue.computed(() => {
    const checkedLength = panelState.checked.length;
    return checkedLength > 0 && checkedLength < checkableData.value.length;
  });
  const updateAllChecked = () => {
    const checkableDataKeys = checkableData.value.map((item) => item[propsAlias.value.key]);
    panelState.allChecked = checkableDataKeys.length > 0 && checkableDataKeys.every((item) => panelState.checked.includes(item));
  };
  vue.watch(() => panelState.checked, () => {
    updateAllChecked();
  });
  vue.watch(checkableData, () => {
    updateAllChecked();
  });
  return {
    checkableData,
    checkedSummary,
    isIndeterminate,
    updateAllChecked
  };
};

exports.useCheck = useCheck;
//# sourceMappingURL=use-check.js.map
