"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _useMergedState = _interopRequireDefault(require("rc-util/es/hooks/useMergedState"));
var _icons = require("@wii/icons");
var _button = _interopRequireDefault(require("../button"));
var _utils = require("../utils");
var _Table = _interopRequireDefault(require("../wau-table/Table"));
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const EditableTableActionContext = /*#__PURE__*/_react.default.createContext(undefined);

/** 可编辑表格的按钮 */
function RecordCreator(props) {
  const {
    children,
    record,
    position,
    newRecordType
  } = props;
  const actionRef = (0, _react.useContext)(EditableTableActionContext);
  return /*#__PURE__*/_react.default.cloneElement(children, {
    ...children.props,
    onClick: e => {
      var _actionRef$current, _children$props$onCli, _children$props;
      actionRef === null || actionRef === void 0 || (_actionRef$current = actionRef.current) === null || _actionRef$current === void 0 ? void 0 : _actionRef$current.addEditRecord(record, {
        position,
        newRecordType
      });
      (_children$props$onCli = (_children$props = children.props).onClick) === null || _children$props$onCli === void 0 ? void 0 : _children$props$onCli.call(_children$props, e);
    }
  });
}

/**
 * 可以直接放到 Form 中的可编辑表格
 *
 * @param props
 */
function WauEditableTable(props) {
  var _props$editable;
  const {
    onTableChange,
    maxLength,
    recordCreatorProps,
    ...rest
  } = props;
  const actionRef = (0, _react.useRef)();
  (0, _react.useImperativeHandle)(rest.actionRef, () => actionRef.current, [actionRef.current]);
  const [value, setValue] = (0, _useMergedState.default)(() => props.value || [], {
    value: props.value,
    onChange: props.onChange
  });
  const {
    record,
    position,
    creatorButtonText,
    newRecordType,
    ...restButtonProps
  } = recordCreatorProps || {};
  const isTop = position === 'top';
  const creatorButtonDom = (0, _react.useMemo)(() => {
    if (maxLength && maxLength <= (value === null || value === void 0 ? void 0 : value.length)) {
      return false;
    }
    return recordCreatorProps !== false && /*#__PURE__*/(0, _jsxRuntime.jsx)(RecordCreator, {
      record: (0, _utils.runFunction)(record, value.length) || {},
      position: position,
      newRecordType: newRecordType,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.default, {
        type: "dashed",
        style: {
          display: 'block',
          margin: '10px 0',
          width: '100%'
        },
        icon: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.PlusOutlined, {}),
        ...restButtonProps,
        children: creatorButtonText || '添加一行数据'
      })
    });
  }, [recordCreatorProps, maxLength, value.length]);
  const buttonRenderProps = (0, _react.useMemo)(() => {
    if (!creatorButtonDom) {
      return {};
    }
    if (isTop) {
      return {
        components: {
          header: {
            wrapper: ({
              className,
              children
            }) => {
              var _rest$columns;
              return /*#__PURE__*/(0, _jsxRuntime.jsxs)("thead", {
                className: className,
                children: [children, /*#__PURE__*/(0, _jsxRuntime.jsx)("tr", {
                  children: /*#__PURE__*/(0, _jsxRuntime.jsx)("td", {
                    colSpan: (_rest$columns = rest.columns) === null || _rest$columns === void 0 ? void 0 : _rest$columns.length,
                    children: creatorButtonDom
                  })
                })]
              });
            }
          }
        }
      };
    }
    return {
      tableViewRender: (_, dom) => {
        var _props$tableViewRende, _props$tableViewRende2;
        return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
          children: [(_props$tableViewRende = (_props$tableViewRende2 = props.tableViewRender) === null || _props$tableViewRende2 === void 0 ? void 0 : _props$tableViewRende2.call(props, _, dom)) !== null && _props$tableViewRende !== void 0 ? _props$tableViewRende : dom, creatorButtonDom]
        });
      }
    };
  }, [isTop, creatorButtonDom]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(EditableTableActionContext.Provider, {
    value: actionRef,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Table.default, {
      search: false,
      options: false,
      pagination: false,
      ...rest,
      ...buttonRenderProps,
      tableLayout: "fixed",
      actionRef: actionRef,
      onChange: onTableChange,
      dataSource: value,
      editable: {
        ...props.editable,
        onValuesChange: props !== null && props !== void 0 && props.onValuesChange || (_props$editable = props.editable) !== null && _props$editable !== void 0 && _props$editable.onValuesChange ? (r, dataSource) => {
          var _props$editable2, _props$editable2$onVa, _props$onValuesChange;
          (_props$editable2 = props.editable) === null || _props$editable2 === void 0 || (_props$editable2$onVa = _props$editable2.onValuesChange) === null || _props$editable2$onVa === void 0 ? void 0 : _props$editable2$onVa.call(_props$editable2, r, dataSource);
          (_props$onValuesChange = props.onValuesChange) === null || _props$onValuesChange === void 0 ? void 0 : _props$onValuesChange.call(props, dataSource, r);
        } : undefined
      },
      onDataSourceChange: setValue
    })
  });
}
WauEditableTable.RecordCreator = RecordCreator;
var _default = WauEditableTable;
exports.default = _default;