var _excluded = ["columns", "dataSource", "operationContent"];
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useEffect, useRef } from 'react';
import { useUpdateEffect } from 'ahooks';
import Table from "../../table";
import useVariables from "../hooks/useVariables";
import ActionsProvider from "../provider/actions/ActionsProvider";
import useDrawerState from "./hooks/useDrawerState";
import useDesignMode from "./hooks/useDesignMode";
import useComponentId from "../hooks/useComponentId";
import useTableQuery from "./hooks/useTableQuery";
import useDataSource from "../hooks/useDataSource";
import TableSettingProvider from "./provider/tableSetting/TableSettingProvider";
import useDataSourceKey from "./hooks/useDataSourceKey";
import useTableProps from "./hooks/useTableProps";
var BaseTable = function BaseTable(props) {
  var propsColumns = props.columns,
    dataSource = props.dataSource,
    operationContent = props.operationContent,
    others = _objectWithoutProperties(props, _excluded);
  var tableRef = useRef(null);
  var componentId = useComponentId(props);
  var _useVariables = useVariables(),
    registerValueVariable = _useVariables.registerValueVariable;
  var _useDataSource = useDataSource(),
    list = _useDataSource.list;
  var data = list.data;
  var _useDataSourceKey = useDataSourceKey(),
    dataSourceKey = _useDataSourceKey.dataSourceKey,
    dataSourceKeyRef = _useDataSourceKey.dataSourceKeyRef;
  var _useDrawerState = useDrawerState(operationContent),
    drawerVisible = _useDrawerState.drawerVisible,
    setDrawerVisible = _useDrawerState.setDrawerVisible,
    openDrawer = _useDrawerState.openDrawer,
    closeDrawer = _useDrawerState.closeDrawer,
    containerProps = _useDrawerState.containerProps,
    Container = _useDrawerState.Container,
    drawerContent = _useDrawerState.drawerContent;
  var _useTableQuery = useTableQuery(props),
    queryPageConfig = _useTableQuery.queryPageConfig,
    handleValuesChange = _useTableQuery.handleValuesChange,
    refreshData = _useTableQuery.refreshData,
    handleFilterChange = _useTableQuery.handleFilterChange;

  // 设计模式下对抽屉的显隐处理
  useDesignMode(props, {
    registerValueVariable: registerValueVariable,
    openDrawer: openDrawer,
    closeDrawer: closeDrawer,
    data: data === null || data === void 0 ? void 0 : data.data
  });
  useUpdateEffect(function () {
    refreshData();
  }, [dataSourceKey]);
  useEffect(function () {
    if (dataSourceKeyRef.current) {
      list === null || list === void 0 ? void 0 : list.run(queryPageConfig);
    } else {
      list === null || list === void 0 ? void 0 : list.mutate([]);
    }
  }, [queryPageConfig]);
  useEffect(function () {
    registerValueVariable === null || registerValueVariable === void 0 ? void 0 : registerValueVariable(componentId, {
      currentDataSource: dataSource
    });
  }, [dataSource]);

  // 关闭抽屉
  var handleCloseDrawer = function handleCloseDrawer() {
    closeDrawer();
    registerValueVariable === null || registerValueVariable === void 0 ? void 0 : registerValueVariable(componentId, {
      currentRecord: null
    });
  };
  var transformProps = useTableProps({
    data: data,
    originProps: props,
    openDrawer: openDrawer,
    closeDrawer: closeDrawer,
    refreshData: refreshData,
    handleFilterChange: handleFilterChange,
    tableRef: tableRef
  });
  return /*#__PURE__*/React.createElement(TableSettingProvider, null, /*#__PURE__*/React.createElement(Table, _extends({
    ref: tableRef
  }, others, transformProps, {
    dataSource: (data === null || data === void 0 ? void 0 : data.data) || [],
    loading: list === null || list === void 0 ? void 0 : list.loading,
    onValuesChange: handleValuesChange
  })), /*#__PURE__*/React.createElement(ActionsProvider, {
    visible: drawerVisible,
    setVisible: setDrawerVisible,
    refreshTableData: refreshData
  }, /*#__PURE__*/React.createElement(Container, _extends({}, containerProps, {
    // title={DRAWER_TITLES[drawerType]}
    open: drawerVisible,
    onClose: handleCloseDrawer,
    onCancel: handleCloseDrawer,
    destroyOnClose: true
  }), drawerContent)));
};
export default BaseTable;