function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import React, { memo, useCallback, useEffect, useMemo, useRef } from 'react';
import { Form } from 'antd';
// import { Access } from '@umijs/max';
import { useDebounce, useMemoizedFn, useSize } from 'ahooks';
import { Context } from "../model";
// import AddFieldModal from "./AddFieldModal";
import BasicTable from "../BasicTable";
import SortRow from "./tableConfig/SortRow";
import { paginationConfig } from "../../Pagination";
import { getText } from "../../../locales";
import useTransDataSource from "../hooks/useTransDataSource";
import useTransColumns from "../hooks/useTransColumns";
import useGenTableComponents from "../hooks/useGenTableComponents";
import useGenScroll from "../hooks/useGenScroll";
import { useSharedState } from "../../../hooks";
import "./index.less";
var GridViewTable = function GridViewTable(_ref) {
  var _tableProps$paginatio, _tableProps$paginatio2;
  var tableProps = _ref.tableProps,
    filter = _ref.filter,
    setTableSetting = _ref.setTableSetting,
    onDataSourceChange = _ref.onDataSourceChange;
  var _useSharedState = useSharedState(Context),
    state = _useSharedState.state,
    dispatch = _useSharedState.dispatch;
  var form = Form.useFormInstance();
  var column_setting = Form.useWatch('column_setting', {
    form: form,
    preserve: true
  }) || [];
  var tableWrapRef = useRef(null);
  var defaultDataSource = state.dataSource,
    originDataSource = state.originDataSource,
    isTerminal = state.isTerminal;
  var handleSave = useMemoizedFn(function (row) {
    var newData = _toConsumableArray(defaultDataSource);
    var index = newData.findIndex(function (item) {
      return row.key === item.key;
    });
    var item = newData[index];
    newData.splice(index, 1, _objectSpread(_objectSpread({}, item), row));
    dispatch({
      type: 'setDataSource',
      payload: newData
    });
  });
  var handleResize = function handleResize(index, record) {
    return function (e, _ref2) {
      var size = _ref2.size;
      var findIndex = column_setting.findIndex(function (item) {
        return item.dataIndex === record.dataIndex;
      });
      var nextColumns = _toConsumableArray(column_setting);
      nextColumns[findIndex] = _objectSpread(_objectSpread({}, nextColumns[findIndex]), {}, {
        width: size.width
      });
      form.setFieldValue('column_setting', nextColumns);
      setTableSetting(form.getFieldsValue());
    };
  };
  var _useTransColumns = useTransColumns({
      columns: column_setting,
      form: form,
      localPagination: !!(tableProps !== null && tableProps !== void 0 && (_tableProps$paginatio = tableProps.pagination) !== null && _tableProps$paginatio !== void 0 && _tableProps$paginatio.localPagination),
      handleResize: handleResize,
      handleSave: handleSave,
      dragSort: tableProps === null || tableProps === void 0 ? void 0 : tableProps.dragSort,
      bordered: tableProps.bordered
    }),
    columns = _useTransColumns.columns;
  var dataSource = useTransDataSource({
    dataSource: originDataSource,
    form: form,
    localPagination: !!(tableProps !== null && tableProps !== void 0 && (_tableProps$paginatio2 = tableProps.pagination) !== null && _tableProps$paginatio2 !== void 0 && _tableProps$paginatio2.localPagination),
    rowKey: tableProps.rowKey,
    filter: filter
  });
  useEffect(function () {
    dispatch({
      type: 'setOriginResultDataSource',
      payload: dataSource || []
    });
  }, [dataSource]);
  useEffect(function () {
    onDataSourceChange === null || onDataSourceChange === void 0 || onDataSourceChange(dataSource);
  }, [JSON.stringify(dataSource)]);
  var components = useGenTableComponents({
    form: form
  });
  console.log(columns, 'columns12');
  var showTotal = useCallback(function (total, range, pageSize) {
    var start = range[0] - 1;
    var _current = start / pageSize + 1;
    return getText('table-pagination-total')(_current, Math.ceil(total / pageSize));
  }, []);
  var _paginationConfig = useMemo(function () {
    if (tableProps.pagination === false) {
      return false;
    }
    return _objectSpread(_objectSpread(_objectSpread({}, paginationConfig), tableProps.pagination), {}, {
      showTotal: showTotal
    });
  }, [tableProps.pagination]);
  return /*#__PURE__*/React.createElement("div", {
    className: 'materials-grid-table-wrap',
    ref: tableWrapRef
  }, /*#__PURE__*/React.createElement(AutoSizerContent, {
    isTerminal: isTerminal,
    tableWrapRef: tableWrapRef,
    dataSource: dataSource,
    tableProps: tableProps,
    columns: columns,
    paginationConfig: _paginationConfig,
    components: components
  }));
};
var AutoSizerContent = function AutoSizerContent(props) {
  var _tableProps$paginatio8, _tableProps$paginatio9, _tableProps$paginatio10;
  var tableProps = props.tableProps,
    dataSource = props.dataSource,
    paginationConfig = props.paginationConfig,
    columns = props.columns,
    components = props.components,
    tableWrapRef = props.tableWrapRef,
    isTerminal = props.isTerminal;
  var _ref3 = useSize(tableWrapRef) || {},
    height = _ref3.height;
  var debouncedHeight = useDebounce(height, {
    wait: 500
  });
  var isShowPagination = useMemo(function () {
    var _tableProps$paginatio3, _tableProps$paginatio4, _tableProps$paginatio5, _tableProps$paginatio6, _tableProps$paginatio7;
    var total = (tableProps === null || tableProps === void 0 || (_tableProps$paginatio3 = tableProps.pagination) === null || _tableProps$paginatio3 === void 0 ? void 0 : _tableProps$paginatio3.total) || (dataSource === null || dataSource === void 0 ? void 0 : dataSource.length) || 0;
    if (tableProps !== null && tableProps !== void 0 && (_tableProps$paginatio4 = tableProps.pagination) !== null && _tableProps$paginatio4 !== void 0 && _tableProps$paginatio4.localPagination) {
      total = (dataSource === null || dataSource === void 0 ? void 0 : dataSource.length) || 0;
    }
    return total > ((tableProps === null || tableProps === void 0 || (_tableProps$paginatio5 = tableProps.pagination) === null || _tableProps$paginatio5 === void 0 ? void 0 : _tableProps$paginatio5.pageSize) || (tableProps === null || tableProps === void 0 || (_tableProps$paginatio6 = tableProps.pagination) === null || _tableProps$paginatio6 === void 0 || (_tableProps$paginatio6 = _tableProps$paginatio6.pageSizeOptions) === null || _tableProps$paginatio6 === void 0 ? void 0 : _tableProps$paginatio6[0]) || 10) || (tableProps === null || tableProps === void 0 || (_tableProps$paginatio7 = tableProps.pagination) === null || _tableProps$paginatio7 === void 0 ? void 0 : _tableProps$paginatio7.showPagination) && total > 10;
  }, [tableProps === null || tableProps === void 0 || (_tableProps$paginatio8 = tableProps.pagination) === null || _tableProps$paginatio8 === void 0 ? void 0 : _tableProps$paginatio8.localPagination, tableProps === null || tableProps === void 0 || (_tableProps$paginatio9 = tableProps.pagination) === null || _tableProps$paginatio9 === void 0 ? void 0 : _tableProps$paginatio9.total, dataSource.length, tableProps === null || tableProps === void 0 || (_tableProps$paginatio10 = tableProps.pagination) === null || _tableProps$paginatio10 === void 0 || (_tableProps$paginatio10 = _tableProps$paginatio10.pageSizeOptions) === null || _tableProps$paginatio10 === void 0 ? void 0 : _tableProps$paginatio10[0]]);
  var scroll = useGenScroll({
    wrapHeight: height || 500,
    scroll: tableProps.scroll,
    columns: columns,
    isShowPagination: isShowPagination
  });
  var debouncedScroll = useDebounce(scroll, {
    wait: 200
  });
  var platformScroll = useMemo(function () {
    if (isTerminal) {
      return debouncedScroll;
    }
    return scroll;
  }, [debouncedScroll, scroll, isTerminal]);
  var basicTable = useMemo(function () {
    if (!height) {
      return null;
    }
    return /*#__PURE__*/React.createElement(BasicTable, _extends({
      dataSource: dataSource,
      columns: columns,
      className: 'grid-view-table',
      components: components
    }, tableProps, {
      scroll: platformScroll,
      pagination: paginationConfig
    }));
  }, [height, platformScroll, dataSource, columns, components, paginationConfig, tableProps]);
  return /*#__PURE__*/React.createElement(SortRow, {
    rowKey: tableProps.rowKey,
    onSortChange: tableProps.onSortChange
  }, basicTable);
};
export default /*#__PURE__*/memo(GridViewTable);