"use strict";

exports.__esModule = true;
exports.default = exports.buildHeaderAndRowFromConfig = void 0;

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

var _reactIntl = require("react-intl");

var _safeGet = _interopRequireDefault(require("../../../utils/safeGet"));

var _Checkbox = _interopRequireDefault(require("../Inputs/Checkbox"));

var _Switch = _interopRequireDefault(require("../Inputs/Switch"));

var _SwitchProps = _interopRequireDefault(require("../Inputs/SwitchProps"));

var _CheckboxProps = _interopRequireDefault(require("../Inputs/CheckboxProps"));

var _TableHeaderCell = _interopRequireDefault(require("./TableHeaderCell"));

var _StandaloneRadio = _interopRequireDefault(require("../Inputs/StandaloneRadio"));

var _standaloneRadioProps = _interopRequireDefault(require("../Inputs/standaloneRadioProps"));

var _TooltippedTypography = _interopRequireDefault(require("./TooltippedElements/TooltippedTypography"));

var _InputBase = _interopRequireDefault(require("./../Inputs/InputBase"));

var _InputBaseProps = _interopRequireDefault(require("./../Inputs/InputBaseProps"));

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

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

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

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var defaultRendering = function defaultRendering(e, def, rowId, readOnly, transformedValue) {
  return transformedValue != null ? /*#__PURE__*/_react.default.createElement(_TooltippedTypography.default, {
    noWrap: true,
    children: transformedValue,
    titleValue: transformedValue
  }) : null;
};

var renderByType = function renderByType(e, def, rowId, readOnly, transformedValue) {
  var _def$transform, _def$switch, _def$switch2, _def$transform2, _def$transform3, _def$switch3, _def$transform4, _def$transform5, _def$transform6, _def$transform7, _def$transform8;

  switch (def.type) {
    case "custom":
      {
        return [def.builder(e, readOnly, def) || defaultRendering(e, def, rowId, readOnly, transformedValue)];
      }

    case "currency":
      {
        var currency = Array.isArray(def.currency) ? _safeGet.default.apply(void 0, [e].concat(def.currency)) : def.currency;

        var currencyValue = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedNumber, {
          style: "currency" // eslint-disable-line react/style-prop-object
          ,
          currency: currency,
          value: transformedValue || "0"
        });

        var tooltippedCurrencyValue = /*#__PURE__*/_react.default.createElement(_TooltippedTypography.default, {
          noWrap: true,
          children: currencyValue,
          titleValue: currencyValue
        });

        return currency != null ? [tooltippedCurrencyValue] : [null];
      }

    case "date":
      var dateValue = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedDate, {
        value: transformedValue
      });

      var tooltippedDateValue = /*#__PURE__*/_react.default.createElement(_TooltippedTypography.default, {
        noWrap: true,
        children: dateValue,
        titleValue: dateValue
      });

      return transformedValue != null ? [tooltippedDateValue] : [null];

    case "datetime":
      var datetimeValue = /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedDate, {
        value: transformedValue
      }), " ", /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedTime, {
        value: transformedValue
      }));

      var tooltippedDatetimeValue = /*#__PURE__*/_react.default.createElement(_TooltippedTypography.default, {
        noWrap: true,
        children: datetimeValue,
        titleValue: datetimeValue
      });

      return transformedValue != null ? [tooltippedDatetimeValue] : [null];

    case "select":
      var checkboxProps = new _CheckboxProps.default();
      checkboxProps.set(_CheckboxProps.default.propNames.update, def.onChange);
      checkboxProps.set(_CheckboxProps.default.propNames.value, !!transformedValue);
      checkboxProps.set(_CheckboxProps.default.propNames.readOnly, ((_def$transform = def.transform) == null ? void 0 : _def$transform.readOnly) != null ? def.transform.readOnly(e, readOnly, def) : readOnly);
      checkboxProps.set(_CheckboxProps.default.propNames.metadata, {
        id: rowId,
        name: def.fieldName
      });
      return [/*#__PURE__*/_react.default.createElement(_Checkbox.default, {
        id: "select_" + transformedValue,
        "data-row-id": rowId,
        checkboxProps: checkboxProps
      }), null];

    case "switch":
      var switchProps = new _SwitchProps.default();
      switchProps.set(_SwitchProps.default.propNames.value, def.reversed ? !transformedValue : !!transformedValue);
      switchProps.set(_SwitchProps.default.propNames.onCaption, (_def$switch = def.switch) == null ? void 0 : _def$switch.onCaption);
      switchProps.set(_SwitchProps.default.propNames.offCaption, (_def$switch2 = def.switch) == null ? void 0 : _def$switch2.offCaption);
      switchProps.set(_SwitchProps.default.propNames.readOnly, ((_def$transform2 = def.transform) == null ? void 0 : _def$transform2.readOnly) != null ? def.transform.readOnly(e, readOnly, def) : readOnly);
      switchProps.set(_SwitchProps.default.propNames.disabled, ((_def$transform3 = def.transform) == null ? void 0 : _def$transform3.disabled) != null ? def.transform.disabled(e, readOnly, def) : def.disabled);
      switchProps.set(_SwitchProps.default.propNames.update, def.onChange);
      switchProps.set(_SwitchProps.default.propNames.className, (_def$switch3 = def.switch) == null ? void 0 : _def$switch3.className);
      switchProps.set(_SwitchProps.default.propNames.metadata, {
        id: rowId,
        name: def.fieldName
      });
      return transformedValue != null ? [/*#__PURE__*/_react.default.createElement(_Switch.default, {
        "data-row-id": rowId,
        switchProps: switchProps
      })] : [null];

    case "radio":
      var selectedValue = def.selectedValue;
      var onChange = def.onChangeCallback;
      var groupName = def.groupName;
      var radioProps = new _standaloneRadioProps.default();
      radioProps.set(_standaloneRadioProps.default.propNames.checked, ((_def$transform4 = def.transform) == null ? void 0 : _def$transform4.checked) != null ? def.transform.checked(e, readOnly, def) : selectedValue === transformedValue);
      radioProps.set(_standaloneRadioProps.default.propNames.value, transformedValue);
      radioProps.set(_standaloneRadioProps.default.propNames.name, ((_def$transform5 = def.transform) == null ? void 0 : _def$transform5.name) != null ? def.transform.name(e, readOnly, def) : groupName);
      radioProps.set(_standaloneRadioProps.default.propNames.onChange, onChange);
      radioProps.set(_standaloneRadioProps.default.propNames.readOnly, ((_def$transform6 = def.transform) == null ? void 0 : _def$transform6.readOnly) != null ? def.transform.readOnly(e, readOnly, def) : readOnly);
      return [/*#__PURE__*/_react.default.createElement(_StandaloneRadio.default, {
        radioProps: radioProps
      })];

    case "textInput":
      var inputBaseProps = new _InputBaseProps.default();
      inputBaseProps.set(_InputBaseProps.default.propNames.value, transformedValue);
      inputBaseProps.set(_InputBaseProps.default.propNames.disabled, ((_def$transform7 = def.transform) == null ? void 0 : _def$transform7.disabled) != null ? def.transform.disabled(e, readOnly, def) : def.disabled);
      inputBaseProps.set(_InputBaseProps.default.propNames.update, def.onChange);
      inputBaseProps.set(_InputBaseProps.default.propNames.placeholder, def.placeholder);
      inputBaseProps.set(_InputBaseProps.default.propNames.error, ((_def$transform8 = def.transform) == null ? void 0 : _def$transform8.error) != null ? def.transform.error(e, readOnly, def) : def.error);
      inputBaseProps.set(_InputBaseProps.default.propNames.inputAttributes, def.inputAttributes);
      inputBaseProps.set(_InputBaseProps.default.propNames.metadata, {
        id: rowId,
        name: def.fieldName
      });
      return [/*#__PURE__*/_react.default.createElement(_InputBase.default, {
        inputProps: inputBaseProps
      })];

    default:
      return [defaultRendering(e, def, rowId, readOnly, transformedValue)];
  }
};

var renderByTypeInEditingMode = function renderByTypeInEditingMode(e, def, rowId, readOnly, transformedValue) {
  if ((def.editingBuilder || null) !== null) return [def.editingBuilder(e, readOnly, def) || defaultRendering(e, def, rowId, readOnly, transformedValue)];
  return renderByType(e, def, rowId, readOnly, transformedValue);
};

var buildHeaderAndRowFromConfig = function buildHeaderAndRowFromConfig(columnDefinitions, elements, readOnly, keyField, showCustomClass) {
  if (readOnly === void 0) {
    readOnly = true;
  }

  if (keyField === void 0) {
    keyField = "id";
  }

  if (showCustomClass === void 0) {
    showCustomClass = false;
  }

  if (columnDefinitions.filter(function (def) {
    return def.sortOptions != null && def.sortOptions.sortField;
  }).length > 1) {
    throw new Error("Only one active sort column can exist at the same time");
  }

  var headers = columnDefinitions.filter(function (def) {
    return def.visible !== false;
  }).map(function (def) {
    return {
      cellElement: /*#__PURE__*/_react.default.createElement(_TableHeaderCell.default, {
        columnDefinition: def
      }),
      className: def.className
    };
  });
  var rows = elements.map(function (e) {
    var rowId = e[keyField];
    var elementClass = e.customClass || "";
    return {
      key: rowId,
      element: e,
      style: {
        show: showCustomClass,
        customClass: elementClass
      },
      columns: columnDefinitions.filter(function (def) {
        return def.visible !== false;
      }).map(function (def) {
        var _def$transform9;

        var transformedValue = (_def$transform9 = def.transform) != null && _def$transform9.value ? def.transform.value(e[def.fieldName]) : e[def.fieldName];

        var _ref = readOnly ? renderByType(e, def, rowId, readOnly, transformedValue) : renderByTypeInEditingMode(e, def, rowId, readOnly, transformedValue),
            _ref2 = _slicedToArray(_ref, 2),
            cellElement = _ref2[0],
            title = _ref2[1];

        return {
          title: def.tooltipable !== false ? title === undefined ? cellElement : title : null,
          cellElement: cellElement,
          className: def.className
        };
      })
    };
  });
  return {
    headers: headers,
    rows: rows
  };
};

exports.buildHeaderAndRowFromConfig = buildHeaderAndRowFromConfig;
var _default = buildHeaderAndRowFromConfig;
var _default2 = _default;
var _default3 = _default2;
exports.default = _default3;
;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(defaultRendering, "defaultRendering", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(renderByType, "renderByType", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(renderByTypeInEditingMode, "renderByTypeInEditingMode", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(buildHeaderAndRowFromConfig, "buildHeaderAndRowFromConfig", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();

;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(defaultRendering, "defaultRendering", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(renderByType, "renderByType", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(renderByTypeInEditingMode, "renderByTypeInEditingMode", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(buildHeaderAndRowFromConfig, "buildHeaderAndRowFromConfig", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/MaterialUI/DataDisplay/tableHelpers.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();