"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderCell = exports.renderColumnsTitle = void 0;

var _react = _interopRequireDefault(require("react"));

var _utils = require("@teamix/utils");

var _proActions = require("@teamix/pro-actions");

var _proField = _interopRequireDefault(require("@teamix/pro-field"));

var _Filter = _interopRequireDefault(require("../components/Filter"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 _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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/**
 * 增加了 icon 的功能 render title
 */
var renderColumnsTitle = function renderColumnsTitle(item, actionRef) {
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_utils.LabelIconTip, {
    label: item.title,
    tooltip: item.tooltip,
    icon: item.tooltipIcon
  }), item.filters && /*#__PURE__*/_react.default.createElement(_Filter.default, {
    column: item,
    actionRef: actionRef
  }));
};
/**
 * 负责单元格的具体渲染
 */


exports.renderColumnsTitle = renderColumnsTitle;

var renderCell = function renderCell(value, item, index, record, actionRef) {
  var _actionSchema$actions;

  var _item$valueType = item.valueType,
      valueType = _item$valueType === void 0 ? 'text' : _item$valueType,
      render = item.render,
      actionSchema = item.actionSchema,
      dataIndex = item.dataIndex; // 如果没传 dataIndex，返回 null

  if (!dataIndex) {
    value = null;
  } // 处理 dataIndex 为数组的情况


  if (Array.isArray(dataIndex)) {
    value = dataIndex.map(function (item) {
      return (0, _utils.getTargetValue)("{{".concat(item, "}}"), _objectSpread(_objectSpread({}, record), {}, {
        record: record
      }));
    });
  }

  var newRender = null; // 如果 render 直接传函数

  if (typeof render === 'function') {
    newRender = function newRender() {
      return render === null || render === void 0 ? void 0 : render(value, index, record);
    };
  } else {
    newRender = processBuriedPoint(processRenderFunction(render, value, index, record), record);
  } // 渲染操作组


  if (actionSchema && ((_actionSchema$actions = actionSchema.actions) === null || _actionSchema$actions === void 0 ? void 0 : _actionSchema$actions.length)) {
    // 默认 context
    var defaultContext = {
      value: value,
      index: index,
      record: record,
      action: actionRef.current
    };
    return /*#__PURE__*/_react.default.createElement(_proActions.ActionGroup, _objectSpread(_objectSpread({
      type: "text"
    }, actionSchema), {}, {
      context: _objectSpread(_objectSpread({}, defaultContext), actionSchema.context)
    }));
  } // 渲染 ProField


  if (render) {
    return /*#__PURE__*/_react.default.createElement(_proField.default, {
      type: valueType,
      value: value,
      render: newRender,
      options: item.options,
      valueEnum: item.valueEnum
    });
  }

  return /*#__PURE__*/_react.default.createElement(_proField.default, {
    type: valueType || 'text',
    value: value,
    // 默认 ellipsis、descriptionEllipsis 为 true
    render: {
      ellipsis: true,
      descriptionEllipsis: true
    }
  });
};
/**
 * 处理 render 配置项是函数的情况
 * @param render ProFieldRender
 * @param value table cell value
 * @param index table cell index
 * @param record table cell record
 * @returns
 */


exports.renderCell = renderCell;

var processRenderFunction = function processRenderFunction() {
  var render = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var value = arguments.length > 1 ? arguments[1] : undefined;
  var index = arguments.length > 2 ? arguments[2] : undefined;
  var record = arguments.length > 3 ? arguments[3] : undefined;
  var external = ['linkOnClick', 'link', 'value'];
  return Object.fromEntries(Object.entries(render).map(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
        k = _ref2[0],
        v = _ref2[1];

    if (typeof v === 'function') {
      var _v;

      if (external.includes(k)) {
        return [k, function () {
          return v === null || v === void 0 ? void 0 : v(value, index, record);
        }];
      }

      return [k, (_v = v === null || v === void 0 ? void 0 : v(value, index, record)) !== null && _v !== void 0 ? _v : ''];
    }

    return [k, v];
  }));
};
/**
 * 处理 render 数据，取出 ${{xxx}} 匹配值
 * @param render ProFieldRender
 * @param record table cell record
 * @returns
 */


var processBuriedPoint = function processBuriedPoint() {
  var render = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var record = arguments.length > 1 ? arguments[1] : undefined;
  return Object.fromEntries([// 默认 ellipsis、descriptionEllipsis 为 true
  ['ellipsis', true], ['descriptionEllipsis', true]].concat(_toConsumableArray(Object.entries(render).map(function (_ref3) {
    var _ref4 = _slicedToArray(_ref3, 2),
        k = _ref4[0],
        v = _ref4[1];

    return [k, (0, _utils.getTargetValue)(v, _objectSpread(_objectSpread({}, record), {}, {
      record: record
    }))];
  }))));
};