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 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); }
import React, { useCallback, useMemo } from "react";
import { Form } from "antd";
import { isBoolean } from "@pisell/utils";
import getColumnNumberProps from "../TableFilter/numberFilter";
import getColumnStringProps from "../TableFilter/stringFilter";
import getColumnSelectProps from "../TableFilter/selectFilter";
import SortIcon from "../TableFilter/SortIcon";
import { getValueByKeys } from "../Table/fields/utils";
import { getText } from "../../../locales";
var genOnCell = function genOnCell(_ref) {
  var bordered = _ref.bordered,
    position = _ref.position;
  return function (_) {
    var styleObj = {};
    var notGroupStyle = {};
    var borderStr = bordered ? "1px solid #EAECF0" : "";
    if (position === "start") {
      styleObj = {
        borderLeft: borderStr,
        borderRadius: "8px 0px 0px 0px",
        padding: "12px"
      };
      notGroupStyle = {
        borderLeft: borderStr
      };
    } else if (position === "end") {
      styleObj = {
        borderRight: borderStr,
        borderRadius: "0px 8px 0px 0px"
      };
      notGroupStyle = {
        borderRight: borderStr
      };
    }
    return {
      style: _.__is_group ? _objectSpread({
        borderTop: "1px solid var(--Gray-200, #EAECF0)"
      }, styleObj) : _objectSpread({}, notGroupStyle)
    };
  };
};
var useTransColumns = function useTransColumns(params) {
  var _group$;
  var columns = params.columns,
    form = params.form,
    handleSave = params.handleSave,
    handleResize = params.handleResize,
    dragSort = params.dragSort,
    bordered = params.bordered;
  var group = Form.useWatch("group_by", {
    form: form,
    preserve: true
  });
  var groupField = group === null || group === void 0 || (_group$ = group[0]) === null || _group$ === void 0 ? void 0 : _group$.field;
  var groupFieldRender = useCallback(function (text, record) {
    if (!groupField) return undefined;
    var children = record.children;
    var fieldCol = columns.find(function (item) {
      return "".concat(item.dataIndex) === groupField;
    });
    var _ref2 = fieldCol || {},
      render = _ref2.render,
      dataIndex = _ref2.dataIndex;
    var dom = /*#__PURE__*/React.createElement("div", null, getValueByKeys(children === null || children === void 0 ? void 0 : children[0], dataIndex || groupField.split(",")));
    if (render) {
      dom = render(getValueByKeys(children === null || children === void 0 ? void 0 : children[0], dataIndex), record.children[0]);
    }
    return /*#__PURE__*/React.createElement("div", {
      style: {
        display: "flex",
        justifyContent: "space-between",
        alignItems: "start"
      }
    }, /*#__PURE__*/React.createElement("span", null, dom), /*#__PURE__*/React.createElement("span", null, children.length, " ", getText("table-column-group-records")));
  }, [groupField, columns]);
  var _columns = useMemo(function () {
    var _newColumns$;
    // 过滤掉不显示的列
    var newColumns = columns.filter(function (item) {
      return !(isBoolean(item.isShow) && !item.isShow);
    });

    // 分组后不展示拖拽排序
    if (dragSort && !groupField && (newColumns === null || newColumns === void 0 || (_newColumns$ = newColumns[0]) === null || _newColumns$ === void 0 ? void 0 : _newColumns$.key) !== "__sort") {
      newColumns.unshift({
        key: "__sort",
        width: 60,
        align: "center",
        className: "drag-sort-cell"
      });
    }
    return newColumns.map(function (col, index) {
      var _col$filters;
      // if (!col.editable) {
      //   return col;
      // }

      var position = "";
      if (index === 0) {
        position = "start";
      } else if (index === newColumns.length - 1) {
        position = "end";
      }
      var groupObj = {};
      if (groupField) {
        groupObj = {
          render: function render(text, record) {
            var _col$render;
            if (record.__is_group) {
              return null;
            }
            return (col === null || col === void 0 || (_col$render = col.render) === null || _col$render === void 0 ? void 0 : _col$render.call(col, text, record)) || text;
          }
        };
      }
      if (position === "start" && groupField) {
        groupObj = {
          fixed: "left",
          width: 350,
          render: function render(text, record) {
            var _col$render2;
            if (record.__is_group && groupFieldRender) {
              return groupFieldRender(text, record);
            }
            return (col === null || col === void 0 || (_col$render2 = col.render) === null || _col$render2 === void 0 ? void 0 : _col$render2.call(col, text, record)) || text;
          }
        };
      }
      var item = {
        editable: col.editable || true,
        dataIndex: col.dataIndex,
        title: col.title,
        field_type: col.field_type,
        lock: col.lock,
        width: col.width || 250,
        action: col.action || "",
        render: col.render,
        contentClassName: col.contentClassName || ""
      };
      var filterObj = {};

      // 开启默认筛选后根据字段类型渲染表头筛选
      if (col.defaultFilters) {
        if (col.field_type === "number") {
          filterObj = getColumnNumberProps({
            dataIndex: col.dataIndex
          });
        }
        if (col.field_type === "text") {
          filterObj = getColumnStringProps({
            dataIndex: col.dataIndex
          });
        }
      }

      // 表头的筛选菜单项
      if (((_col$filters = col.filters) === null || _col$filters === void 0 ? void 0 : _col$filters.length) > 0) {
        filterObj = _objectSpread(_objectSpread({}, getColumnSelectProps({
          dataIndex: col.dataIndex,
          filters: col.filters
        })), {}, {
          filters: col.filters
        });
      }
      return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, col), filterObj), groupObj), {}, {
        sortIcon: function sortIcon(props) {
          return /*#__PURE__*/React.createElement(SortIcon, props);
        },
        onCell: function onCell(record) {
          return _objectSpread(_objectSpread(_objectSpread({
            record: record
          }, item), genOnCell({
            position: position,
            bordered: bordered
          })(record)), {}, {
            handleSave: handleSave
          });
        },
        onHeaderCell: function onHeaderCell(record) {
          return _objectSpread(_objectSpread({
            record: record
          }, item), {}, {
            onResize: handleResize === null || handleResize === void 0 ? void 0 : handleResize(index, record),
            position: position
          });
        }
      });
    });
  }, [columns, groupFieldRender, dragSort, groupField, bordered]);
  return {
    columns: _columns
  };
};
export default useTransColumns;