"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 _proField = _interopRequireDefault(require("@teamix/pro-field"));

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

function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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; }

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


exports.renderColumnsTitle = renderColumnsTitle;

var renderCell = function renderCell(value, item, index, record) {
  var _item$valueType = item.valueType,
      valueType = _item$valueType === void 0 ? 'text' : _item$valueType,
      render = item.render;
  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 (render) {
    return /*#__PURE__*/_react.default.createElement(_proField.default, {
      type: valueType,
      value: value,
      render: newRender,
      options: item.options,
      valueEnum: item.valueEnum
    });
  } else {
    return /*#__PURE__*/_react.default.createElement(_proField.default, {
      type: valueType || 'text',
      value: value
    });
  }
};
/**
 * 处理 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'];
  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(Object.entries(render).map(function (_ref3) {
    var _ref4 = _slicedToArray(_ref3, 2),
        k = _ref4[0],
        v = _ref4[1];

    if (/\$\{.*\}/.test(v)) {
      var _v$match$, _v$match;

      var key = (_v$match$ = (_v$match = v.match(/(?<=\$\{).*?(?=})/g)) === null || _v$match === void 0 ? void 0 : _v$match[0]) !== null && _v$match$ !== void 0 ? _v$match$ : '';
      return [k, renderAttribute(key, record)];
    }

    return [k, v];
  }));
};
/**
 * 匹配 record 数据
 * @param key 例如：title.name
 * @param record table cell record
 * @returns
 */


var renderAttribute = function renderAttribute(key, record) {
  var _key$split;

  var result = '';
  var keyList = (_key$split = key === null || key === void 0 ? void 0 : key.split('.')) !== null && _key$split !== void 0 ? _key$split : [];

  if (keyList.length > 1) {
    var _keyList = _toArray(keyList),
        k1 = _keyList[0],
        remainKey = _keyList.slice(1);

    result = renderAttribute(remainKey.join('.'), record[k1]);
    return result;
  }

  result = record[key];
  return result;
};