function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 ? 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 ? 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 ? 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 ? 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),
            position: position
          });
        }
      });
    });
  }, [columns, groupFieldRender, dragSort, groupField, bordered]);
  return {
    columns: _columns
  };
};
export default useTransColumns;