/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _slicedToArray } from '../_chunks/dep-55846619.mjs';
import { _ as _defineProperty } from '../_chunks/dep-eb20e23f.mjs';
import { defineComponent, toRefs, ref, computed, onMounted, watch, createVNode, mergeProps } from 'vue';
import { g as get_1 } from '../_chunks/dep-4c3e8dd5.mjs';
import { s as set_1 } from '../_chunks/dep-b8611e18.mjs';
import { i as isFunction_1 } from '../_chunks/dep-6b22dd27.mjs';
import { Edit1Icon } from '@ldesign/icons-vue-next';
import { i as isObject_1 } from '../_chunks/dep-0f0c89ff.mjs';
import { useGlobalIcon } from '../hooks/useGlobalIcon.mjs';
import { validate } from '../form/form-model.mjs';
import '../_common/js/log/index.mjs';
import { on, off } from '../utils/dom.mjs';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import { renderCell } from './tr.mjs';
import log from '../_common/js/log/log.mjs';
import '../_chunks/dep-cfcedf47.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-7acc11cb.mjs';
import '../_chunks/dep-e3b14d70.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-ea5448a5.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-c852a61a.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-44417261.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-1cf2b129.mjs';
import '../_chunks/dep-99920870.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../config-provider/type.mjs';
import '../_chunks/dep-beaa677e.mjs';
import '../_chunks/dep-bd03960e.mjs';
import '../_chunks/dep-6eea369f.mjs';
import '../_chunks/dep-0cfc2d3f.mjs';
import '../utils/helper.mjs';
import '../_chunks/dep-b6a73f93.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-30451c48.mjs';
import '../_chunks/dep-6f89b77e.mjs';
import '../utils/easing.mjs';
import '../_chunks/dep-e9bfe96d.mjs';
import '../_chunks/dep-774ac2d0.mjs';
import './utils.mjs';
import './hooks/useFixed.mjs';
import '../_chunks/dep-96bb54d5.mjs';
import '../_chunks/dep-6205fc47.mjs';
import '../_common/js/utils/getScrollbarWidth.mjs';
import '../_common/js/utils/helper.mjs';
import '../_chunks/dep-e091cfd9.mjs';
import './hooks/useClassName.mjs';
import './ellipsis.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-c4e1a112.mjs';
import '../tooltip/index.mjs';
import '../utils/withInstall.mjs';
import '../tooltip/tooltip.mjs';
import '../_chunks/dep-b2af48ba.mjs';
import '../popup/props.mjs';
import '../popup/index.mjs';
import '../popup/popup.mjs';
import '@popperjs/core';
import '../hooks/index.mjs';
import '../hooks/icon.mjs';
import '../hooks/slot.mjs';
import '../hooks/tnode.mjs';
import '../hooks/useCommonClassName.mjs';
import '../hooks/useDefaultValue.mjs';
import '../hooks/useDestroyOnClose.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useVirtualScroll.mjs';
import '../hooks/useVModel.mjs';
import '../_common/js/utils/set-style.mjs';
import '../popup/container.mjs';
import '../hooks/useResizeObserver.mjs';
import '../popup/style/css.mjs';
import '../popup/type.mjs';
import '../tooltip/util.mjs';
import '../tooltip/props.mjs';
import '../tooltip/style/css.mjs';
import '../tooltip/type.mjs';
import './base-table-props.mjs';
import './hooks/useLazyLoad.mjs';
import '../_common/js/utils/observe.mjs';
import './hooks/useRowspanAndColspan.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var EditableCell = defineComponent({
  name: "TableEditableCell",
  props: {
    row: Object,
    rowIndex: Number,
    col: Object,
    colIndex: Number,
    oldCell: [Function, String],
    tableBaseClass: Object,
    cellEmptyContent: [Function, String],
    editable: {
      type: Boolean,
      "default": void 0
    },
    readonly: {
      type: Boolean
    },
    errors: {
      type: Array,
      "default": void 0
    },
    onChange: Function,
    onValidate: Function,
    onRuleChange: Function,
    onEditableChange: Function
  },
  setup: function setup(props, context) {
    var _props$col$edit;
    var _toRefs = toRefs(props),
      row = _toRefs.row,
      col = _toRefs.col;
    var tableEditableCellRef = ref(null);
    var isEdit = ref(((_props$col$edit = props.col.edit) === null || _props$col$edit === void 0 ? void 0 : _props$col$edit.defaultEditable) || false);
    var editValue = ref();
    var errorList = ref();
    var classPrefix = usePrefixClass();
    var _useGlobalIcon = useGlobalIcon({
        Edit1Icon: Edit1Icon
      }),
      Edit1Icon$1 = _useGlobalIcon.Edit1Icon;
    var editOnListeners = computed(function () {
      var _col$value$edit, _col$value$edit$on;
      return ((_col$value$edit = col.value.edit) === null || _col$value$edit === void 0 ? void 0 : (_col$value$edit$on = _col$value$edit.on) === null || _col$value$edit$on === void 0 ? void 0 : _col$value$edit$on.call(_col$value$edit, _objectSpread(_objectSpread({}, cellParams.value), {}, {
        editedRow: currentRow.value
      }))) || {};
    });
    var cellParams = computed(function () {
      return {
        rowIndex: props.rowIndex,
        colIndex: props.colIndex,
        col: props.col,
        row: props.row
      };
    });
    var currentRow = computed(function () {
      var newRow = _objectSpread({}, row.value);
      set_1(newRow, col.value.colKey, editValue.value);
      return newRow;
    });
    var cellNode = computed(function () {
      var node = renderCell({
        row: currentRow.value,
        col: _objectSpread(_objectSpread({}, col.value), {}, {
          cell: props.oldCell
        }),
        rowIndex: props.rowIndex,
        colIndex: props.colIndex
      }, context.slots, {
        cellEmptyContent: props.cellEmptyContent
      });
      return node;
    });
    var componentProps = computed(function () {
      var _edit$abortEditOnEven;
      var edit = col.value.edit;
      if (!edit) return {};
      var editProps = isFunction_1(edit.props) ? edit.props(_objectSpread(_objectSpread({}, cellParams.value), {}, {
        editedRow: currentRow.value
      })) : _objectSpread({}, edit.props);
      delete editProps.onChange;
      delete editProps.value;
      (_edit$abortEditOnEven = edit.abortEditOnEvent) === null || _edit$abortEditOnEven === void 0 ? void 0 : _edit$abortEditOnEven.forEach(function (item) {
        delete editProps[item];
      });
      return editProps;
    });
    var isAbortEditOnChange = computed(function () {
      var _edit$abortEditOnEven2;
      var edit = col.value.edit;
      if (!edit) return false;
      return Boolean((_edit$abortEditOnEven2 = edit.abortEditOnEvent) === null || _edit$abortEditOnEven2 === void 0 ? void 0 : _edit$abortEditOnEven2.includes("onChange"));
    });
    var validateEdit = function validateEdit(trigger) {
      return new Promise(function (resolve) {
        var params = {
          result: [_objectSpread(_objectSpread({}, cellParams.value), {}, {
            errorList: [],
            value: editValue.value
          })],
          trigger: trigger
        };
        var rules = isFunction_1(col.value.edit.rules) ? col.value.edit.rules(cellParams.value) : col.value.edit.rules;
        if (!col.value.edit || !rules || !rules.length) {
          var _props$onValidate;
          (_props$onValidate = props.onValidate) === null || _props$onValidate === void 0 ? void 0 : _props$onValidate.call(props, params);
          resolve(true);
          return;
        }
        validate(editValue.value, rules).then(function (result) {
          var _props$onValidate2;
          var list = result === null || result === void 0 ? void 0 : result.filter(function (t) {
            return !t.result;
          });
          params.result[0].errorList = list;
          (_props$onValidate2 = props.onValidate) === null || _props$onValidate2 === void 0 ? void 0 : _props$onValidate2.call(props, params);
          if (!list || !list.length) {
            errorList.value = [];
            resolve(true);
          } else {
            errorList.value = list;
            resolve(list);
          }
        });
      });
    };
    var isSame = function isSame(a, b) {
      if (isObject_1(a) && isObject_1(b)) return JSON.stringify(a) === JSON.stringify(b);
      return a === b;
    };
    var updateAndSaveAbort = function updateAndSaveAbort(outsideAbortEvent, eventName) {
      for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
        args[_key - 2] = arguments[_key];
      }
      validateEdit("self").then(function (result) {
        var _editOnListeners$valu, _editOnListeners$valu2;
        if (result !== true) return;
        var oldValue = get_1(row.value, col.value.colKey);
        if (!isSame(editValue.value, oldValue)) {
          editValue.value = oldValue;
          outsideAbortEvent === null || outsideAbortEvent === void 0 ? void 0 : outsideAbortEvent.apply(void 0, args);
        }
        (_editOnListeners$valu = (_editOnListeners$valu2 = editOnListeners.value)[eventName]) === null || _editOnListeners$valu === void 0 ? void 0 : _editOnListeners$valu.call(_editOnListeners$valu2, args[2]);
        var timer = setTimeout(function () {
          var _props$onEditableChan;
          isEdit.value = false;
          errorList.value = [];
          (_props$onEditableChan = props.onEditableChange) === null || _props$onEditableChan === void 0 ? void 0 : _props$onEditableChan.call(props, _objectSpread(_objectSpread({}, cellParams.value), {}, {
            value: editValue.value,
            editedRow: _objectSpread(_objectSpread({}, props.row), {}, _defineProperty({}, props.col.colKey, editValue.value)),
            validateEdit: validateEdit,
            isEdit: false
          }));
          clearTimeout(timer);
        }, 0);
      });
    };
    var listeners = computed(function () {
      var _edit$abortEditOnEven3;
      var edit = col.value.edit;
      var isCellEditable = props.editable === void 0;
      if (!isEdit.value || !isCellEditable) return;
      if (!(edit !== null && edit !== void 0 && (_edit$abortEditOnEven3 = edit.abortEditOnEvent) !== null && _edit$abortEditOnEven3 !== void 0 && _edit$abortEditOnEven3.length)) return {};
      var tListeners = {};
      var outsideAbortEvent = edit === null || edit === void 0 ? void 0 : edit.onEdited;
      edit.abortEditOnEvent.forEach(function (itemEvent) {
        if (itemEvent === "onChange") return;
        tListeners[itemEvent] = function () {
          for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
            args[_key2] = arguments[_key2];
          }
          updateAndSaveAbort.apply(void 0, [outsideAbortEvent, itemEvent, _objectSpread(_objectSpread({}, cellParams.value), {}, {
            trigger: itemEvent,
            newRowData: currentRow.value
          })].concat(args));
        };
      });
      return tListeners;
    });
    var onEditChange = function onEditChange(val) {
      var _props$onChange, _props$onRuleChange, _editOnListeners$valu3, _editOnListeners$valu4, _col$value$edit3;
      editValue.value = val;
      var params = _objectSpread(_objectSpread({}, cellParams.value), {}, {
        value: val,
        editedRow: _objectSpread(_objectSpread({}, props.row), {}, _defineProperty({}, props.col.colKey, val))
      });
      (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, params);
      (_props$onRuleChange = props.onRuleChange) === null || _props$onRuleChange === void 0 ? void 0 : _props$onRuleChange.call(props, params);
      (_editOnListeners$valu3 = editOnListeners.value) === null || _editOnListeners$valu3 === void 0 ? void 0 : (_editOnListeners$valu4 = _editOnListeners$valu3.onChange) === null || _editOnListeners$valu4 === void 0 ? void 0 : _editOnListeners$valu4.call(_editOnListeners$valu3, params);
      var isCellEditable = props.editable === void 0;
      if (isCellEditable && isAbortEditOnChange.value) {
        var _col$value$edit2;
        var outsideAbortEvent = (_col$value$edit2 = col.value.edit) === null || _col$value$edit2 === void 0 ? void 0 : _col$value$edit2.onEdited;
        for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
          args[_key3 - 1] = arguments[_key3];
        }
        updateAndSaveAbort.apply(void 0, [outsideAbortEvent, "change", _objectSpread(_objectSpread({}, cellParams.value), {}, {
          trigger: "onChange",
          newRowData: currentRow.value
        })].concat(args));
      }
      if (((_col$value$edit3 = col.value.edit) === null || _col$value$edit3 === void 0 ? void 0 : _col$value$edit3.validateTrigger) === "change") validateEdit("self");
    };
    var documentClickHandler = function documentClickHandler(e) {
      var _e$composedPath;
      if (!col.value.edit || !col.value.edit.component) return;
      if (!isEdit.value) return;
      var path = ((_e$composedPath = e.composedPath) === null || _e$composedPath === void 0 ? void 0 : _e$composedPath.call(e)) || e.path || [];
      var node = path.find(function (node2) {
        var _node2$classList;
        return (_node2$classList = node2.classList) === null || _node2$classList === void 0 ? void 0 : _node2$classList.contains("".concat(classPrefix.value, "-popup__content"));
      });
      if (node) return;
      var outsideAbortEvent = col.value.edit.onEdited;
      updateAndSaveAbort(outsideAbortEvent, "", _objectSpread(_objectSpread({}, cellParams.value), {}, {
        trigger: "document",
        newRowData: currentRow.value
      }));
    };
    var enterEdit = function enterEdit() {
      var _props$onEditableChan2;
      (_props$onEditableChan2 = props.onEditableChange) === null || _props$onEditableChan2 === void 0 ? void 0 : _props$onEditableChan2.call(props, _objectSpread(_objectSpread({}, cellParams.value), {}, {
        value: editValue.value,
        editedRow: props.row,
        isEdit: true,
        validateEdit: validateEdit
      }));
    };
    var onCellClick = function onCellClick(e) {
      isEdit.value = true;
      enterEdit();
      e.stopPropagation();
    };
    onMounted(function () {
      var _props$col$edit2;
      if ((_props$col$edit2 = props.col.edit) !== null && _props$col$edit2 !== void 0 && _props$col$edit2.defaultEditable) enterEdit();
    });
    var cellValue = computed(function () {
      return get_1(row.value, col.value.colKey);
    });
    watch(cellValue, function (cellValue2) {
      editValue.value = cellValue2;
    }, {
      immediate: true
    });
    watch(isEdit, function (isEdit2) {
      var isCellEditable = props.editable === void 0;
      if (!col.value.edit || !col.value.edit.component || !isCellEditable) return;
      if (isEdit2) on(document, "click", documentClickHandler);else off(document, "click", documentClickHandler);
    }, {
      immediate: true
    });
    watch(function () {
      return [props.editable, props.row, props.col, props.rowIndex, props.colIndex];
    }, function (_ref) {
      var _ref2 = _slicedToArray(_ref, 1),
        editable = _ref2[0];
      if (editable === false) {
        editValue.value = cellValue.value;
      } else if (editable === true) {
        var _props$onRuleChange2;
        (_props$onRuleChange2 = props.onRuleChange) === null || _props$onRuleChange2 === void 0 ? void 0 : _props$onRuleChange2.call(props, _objectSpread(_objectSpread({}, cellParams.value), {}, {
          value: cellValue.value,
          editedRow: row.value
        }));
      }
    }, {
      immediate: true
    });
    watch(function () {
      return props.errors;
    }, function (errors) {
      errorList.value = errors;
    });
    return function () {
      var _col$value$edit5, _errorList$value, _errorList$value$, _col$value$edit6, _col$value$edit6$abor, _errorList$value2, _errorList$value2$;
      if (props.readonly) return cellNode.value;
      if (props.editable === void 0 && !isEdit.value || props.editable === false) {
        var _col$value$edit4;
        return createVNode("div", {
          "class": props.tableBaseClass.cellEditable,
          "onClick": onCellClick
        }, [cellNode.value, ((_col$value$edit4 = col.value.edit) === null || _col$value$edit4 === void 0 ? void 0 : _col$value$edit4.showEditIcon) !== false && createVNode(Edit1Icon$1, null, null)]);
      }
      var Component = (_col$value$edit5 = col.value.edit) === null || _col$value$edit5 === void 0 ? void 0 : _col$value$edit5.component;
      if (!Component) {
        log.error("Table", "edit.component is required.");
        return null;
      }
      var errorMessage = (_errorList$value = errorList.value) === null || _errorList$value === void 0 ? void 0 : (_errorList$value$ = _errorList$value[0]) === null || _errorList$value$ === void 0 ? void 0 : _errorList$value$.message;
      var tmpEditOnListeners = _objectSpread({}, editOnListeners.value);
      delete tmpEditOnListeners.onChange;
      if ((_col$value$edit6 = col.value.edit) !== null && _col$value$edit6 !== void 0 && (_col$value$edit6$abor = _col$value$edit6.abortEditOnEvent) !== null && _col$value$edit6$abor !== void 0 && _col$value$edit6$abor.length) {
        col.value.edit.abortEditOnEvent.forEach(function (onEventName) {
          if (tmpEditOnListeners[onEventName]) delete tmpEditOnListeners[onEventName];
        });
      }
      return createVNode("div", {
        "class": props.tableBaseClass.cellEditWrap,
        "onClick": function onClick(e) {
          e.stopPropagation();
        },
        "ref": "tableEditableCellRef"
      }, [createVNode(Component, mergeProps({
        "status": errorMessage ? ((_errorList$value2 = errorList.value) === null || _errorList$value2 === void 0 ? void 0 : (_errorList$value2$ = _errorList$value2[0]) === null || _errorList$value2$ === void 0 ? void 0 : _errorList$value2$.type) || "error" : void 0,
        "tips": errorMessage
      }, componentProps.value, listeners.value, tmpEditOnListeners, {
        "value": editValue.value,
        "onChange": onEditChange
      }), null)]);
    };
  }
});

export { EditableCell as default };
//# sourceMappingURL=editable-cell.mjs.map
