/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { ref, computed } from '@vue/composition-api';
import get from 'lodash/get';
import isFunction from 'lodash/isFunction';
import { getEditableKeysMap } from '../utils.js';
import { validate } from '../../form/model.js';
import 'lodash/isObject';
import '../../../_chunks/dep-74554e14.js';
import 'lodash/isEmpty';
import 'lodash/isNumber';
import '../../../utils/helper.js';
import 'lodash/camelCase';

const cellRuleMap = /* @__PURE__ */new Map();
function useRowEdit(props, context) {
  const errorListMap = ref({});
  const editableKeysMap = computed(() => getEditableKeysMap(props.editableRowKeys, props.data, props.rowKey || "id"));
  const getErrorListMapByErrors = errors => {
    const errorMap = {};
    errors.forEach(_ref => {
      let {
        row,
        col,
        errorList
      } = _ref;
      const rowValue = get(row, props.rowKey || "id");
      const key = [rowValue, col.colKey].join("__");
      if (errorList !== null && errorList !== void 0 && errorList.length) errorMap[key] = errorList;else delete errorMap[key];
    });
    return errorMap;
  };
  const validateOneRowData = rowValue => {
    const rowRules = cellRuleMap.get(rowValue);
    if (!rowRules) return;
    const list = rowRules.map(item => new Promise(resolve => {
      const {
        editedRow,
        col
      } = item;
      const rules = isFunction(col.edit.rules) ? col.edit.rules(item) : col.edit.rules;
      if (!col.edit || !rules || !rules.length) {
        resolve({
          ...item,
          errorList: []
        });
        return;
      }
      validate(editedRow[col.colKey], rules).then(r => {
        resolve({
          ...item,
          errorList: r.filter(t => !t.result)
        });
      });
    }));
    return new Promise((resolve, reject) => {
      Promise.all(list).then(errors => {
        resolve({
          errors: errors.filter(t => {
            var _t$errorList;
            return (_t$errorList = t.errorList) === null || _t$errorList === void 0 ? void 0 : _t$errorList.length;
          }),
          errorMap: getErrorListMapByErrors(errors)
        });
      }, reject);
    });
  };
  const validateRowData = rowValue => new Promise((resolve, reject) => {
    validateOneRowData(rowValue).then(_ref2 => {
      var _props$onRowValidate;
      let {
        errors,
        errorMap
      } = _ref2;
      errorListMap.value = errorMap;
      const tTrigger = "parent";
      (_props$onRowValidate = props.onRowValidate) === null || _props$onRowValidate === void 0 ? void 0 : _props$onRowValidate.call(props, {
        trigger: tTrigger,
        result: errors
      });
      resolve({
        trigger: tTrigger,
        result: errors
      });
    }, reject);
  });
  const validateTableData = () => {
    const promiseList = [];
    const data = props.data || [];
    for (let i = 0, len = data.length; i < len; i++) {
      const rowValue = get(data[i], props.rowKey || "id");
      promiseList.push(validateOneRowData(rowValue));
    }
    return new Promise((resolve, reject) => {
      Promise.all(promiseList).then(rList => {
        var _props$onValidate;
        const allErrorListMap = {};
        rList.forEach(function () {
          let {
            errorMap
          } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
            errors: [],
            errorMap: {}
          };
          errorMap && Object.assign(allErrorListMap, errorMap);
        });
        errorListMap.value = allErrorListMap;
        (_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 ? void 0 : _props$onValidate.call(props, {
          result: allErrorListMap
        });
        resolve({
          result: allErrorListMap
        });
      }, reject);
    });
  };
  const onRuleChange = context2 => {
    if (props.editableRowKeys) {
      const rowValue = get(context2.row, props.rowKey || "id");
      const rules = cellRuleMap.get(rowValue);
      if (rules) {
        const index = rules.findIndex(t => t.col.colKey === context2.col.colKey);
        if (index === -1) rules.push(context2);else rules[index] = context2;
        cellRuleMap.set(rowValue, rules);
      } else {
        cellRuleMap.set(rowValue, [context2]);
      }
    }
  };
  const clearValidateData = () => {
    errorListMap.value = {};
  };
  const onPrimaryTableRowEdit = params => {
    var _props$onRowEdit;
    (_props$onRowEdit = props.onRowEdit) === null || _props$onRowEdit === void 0 ? void 0 : _props$onRowEdit.call(props, params);
    context.emit("row-edit", params);
  };
  const onPrimaryTableRowValidate = params => {
    var _props$onRowValidate2;
    (_props$onRowValidate2 = props.onRowValidate) === null || _props$onRowValidate2 === void 0 ? void 0 : _props$onRowValidate2.call(props, params);
    context.emit("row-validate", params);
  };
  return {
    errorListMap,
    editableKeysMap,
    validateTableData,
    validateRowData,
    onRuleChange,
    clearValidateData,
    onPrimaryTableRowEdit,
    onPrimaryTableRowValidate
  };
}

export { useRowEdit as default };
//# sourceMappingURL=useEditableRow.js.map
