"use strict";

exports.__esModule = true;
exports.default = void 0;
var _isString2 = _interopRequireDefault(require("lodash/fp/isString"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _checkbox = _interopRequireDefault(require("../../atom/checkbox"));
var _link = _interopRequireDefault(require("../../atom/link"));
var _style = _interopRequireDefault(require("./style.css"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const THEMES = {
  COCKPIT: 'cockpit'
};
const createOptionsView = (_options, hasOptions) => {
  const optionsView = _options.map((option, oIndex) => {
    const {
      onChange,
      selected
    } = option;
    return /*#__PURE__*/_react.default.createElement("div", {
      key: oIndex,
      className: selected ? _style.default.selected : _style.default.option
    }, /*#__PURE__*/_react.default.createElement("button", {
      type: "button",
      onClick: onChange
    }, option.title));
  });
  return hasOptions ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.options
  }, optionsView) : null;
};
const buildTableContext = (translate, theme, headerTitle) => {
  if (theme === THEMES.COCKPIT) {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.header
    }, /*#__PURE__*/_react.default.createElement("h1", {
      className: _style.default.title
    }, headerTitle), /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.legend
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.icon
    }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyDraft, {
      width: 25,
      height: 25
    }), /*#__PURE__*/_react.default.createElement("span", {
      className: _style.default.label
    }, translate('Draft'))), /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.icon
    }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyValidate, {
      width: 25,
      height: 25
    }), /*#__PURE__*/_react.default.createElement("span", {
      className: _style.default.label
    }, translate('Validated'))), /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.icon
    }, /*#__PURE__*/_react.default.createElement("span", {
      className: _style.default.nostatus
    }, '\u26A0️'), /*#__PURE__*/_react.default.createElement("span", {
      className: _style.default.label
    }, translate('No status')))));
  }
  if (headerTitle) {
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.header
    }, /*#__PURE__*/_react.default.createElement("h2", {
      className: _style.default.title
    }, headerTitle));
  }
  return null;
};
const Table = (props, context) => {
  const {
    rows = [],
    columns = [],
    editable = true,
    theme,
    headerTitle = ''
  } = props;
  const {
    skin,
    translate
  } = context;
  const mediumColor = (0, _get2.default)('common.medium', skin);
  const headerView = columns.map((column, cIndex) => {
    const {
      title,
      filtered,
      info = '',
      options = []
    } = column;
    const hasOptions = options.length > 0;
    const OptionsIcon = hasOptions && (filtered ? _novaIcons.NovaCompositionCoorpacademyFunnel : _novaIcons.NovaCompositionNavigationArrowDown) || null;
    return /*#__PURE__*/_react.default.createElement("th", {
      title: info,
      key: cIndex
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: hasOptions ? _style.default.toggle : _style.default.noOptions
    }, /*#__PURE__*/_react.default.createElement(_checkbox.default, {
      id: title,
      name: title,
      checked: true,
      className: _style.default.checkbox
    }), /*#__PURE__*/_react.default.createElement("label", {
      htmlFor: title
    }, title), options.length > 0 ? createOptionsView(options, hasOptions) : null, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.optionsIconWrapper
    }, hasOptions ? /*#__PURE__*/_react.default.createElement(OptionsIcon, {
      style: {
        color: mediumColor
      },
      height: 16,
      width: 16,
      className: _style.default.optionsIcon
    }) : null)));
  });
  const tableContext = buildTableContext(translate, theme, headerTitle);
  if (editable) {
    headerView.unshift(/*#__PURE__*/_react.default.createElement("th", {
      key: "header"
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.noOptions
    })));
  }
  const renderIcon = field => {
    switch ((0, _get2.default)('icon', field)) {
      case 'draft':
        return /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyDraft, {
          width: 25,
          height: 25
        });
      case 'validate':
        return /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyValidate, {
          width: 25,
          height: 25
        });
      case 'nostatus':
        return /*#__PURE__*/_react.default.createElement("span", {
          className: _style.default.nostatus
        }, '\u26A0️');
      case 'videosubtitle':
        return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidVideosVideoSubtitle, {
          className: _style.default.videosubtitle,
          width: 25,
          height: 25
        }), field.title);
      default:
        null;
    }
  };
  const bodyView = rows.map((row, index) => {
    const {
      fields = [],
      editHref
    } = row;
    const trClasses = (0, _classnames.default)({
      [_style.default.highlighted]: row.highlighted
    });
    const tableRows = fields.map((field, fIndex) => {
      if (!field) return /*#__PURE__*/_react.default.createElement("td", {
        key: fIndex
      }, '');
      return /*#__PURE__*/_react.default.createElement("td", {
        key: fIndex
      }, (0, _isString2.default)(field) ? field : renderIcon(field));
    });
    if (editable) {
      tableRows.unshift(
      /*#__PURE__*/
      // TODO restore dropped support of hoverColor={brandColor}
      _react.default.createElement("td", {
        key: "header"
      }, /*#__PURE__*/_react.default.createElement(_link.default, {
        href: editHref
      }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidContentEditionPencil1, {
        style: {
          color: mediumColor
        },
        height: 16,
        width: 16
      }))));
    }
    return /*#__PURE__*/_react.default.createElement("tr", {
      className: trClasses,
      key: index
    }, tableRows);
  });
  const mainClass = (0, _classnames.default)(theme === THEMES.COCKPIT ? _style.default.cockpit : _style.default.wrapper);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: mainClass
  }, tableContext, /*#__PURE__*/_react.default.createElement("table", {
    className: (0, _classnames.default)(_style.default.table, {
      [_style.default.readonly]: !editable
    })
  }, /*#__PURE__*/_react.default.createElement("thead", null, /*#__PURE__*/_react.default.createElement("tr", null, headerView)), /*#__PURE__*/_react.default.createElement("tbody", null, bodyView)));
};
Table.contextTypes = {
  skin: _provider.default.childContextTypes.skin,
  translate: _provider.default.childContextTypes.translate
};
Table.propTypes = process.env.NODE_ENV !== "production" ? {
  editable: _propTypes.default.bool,
  rows: _propTypes.default.arrayOf(_propTypes.default.shape({
    fields: _propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.shape({
      icon: _propTypes.default.string
    }), _propTypes.default.shape({
      icon: _propTypes.default.string,
      title: _propTypes.default.string
    })])),
    editHref: _propTypes.default.string,
    highlighted: _propTypes.default.bool
  })),
  columns: _propTypes.default.arrayOf(_propTypes.default.shape({
    title: _propTypes.default.string.isRequired,
    info: _propTypes.default.string,
    filtered: _propTypes.default.bool,
    options: _propTypes.default.arrayOf(_propTypes.default.shape({
      title: _propTypes.default.string.isRequired,
      onChange: _propTypes.default.func.isRequired,
      selected: _propTypes.default.bool.isRequired
    }))
  })),
  theme: _propTypes.default.oneOf([THEMES.COCKPIT]),
  headerTitle: _propTypes.default.string
} : {};
var _default = exports.default = Table;
//# sourceMappingURL=index.js.map