"use strict";

exports.__esModule = true;
exports.default = exports.UploadReport = void 0;
var _includes2 = _interopRequireDefault(require("lodash/fp/includes"));
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _getOr2 = _interopRequireDefault(require("lodash/fp/getOr"));
var _head2 = _interopRequireDefault(require("lodash/fp/head"));
var _map2 = _interopRequireDefault(require("lodash/fp/map"));
var _last2 = _interopRequireDefault(require("lodash/fp/last"));
var _split2 = _interopRequireDefault(require("lodash/fp/split"));
var _pipe2 = _interopRequireDefault(require("lodash/fp/pipe"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _classnames = _interopRequireDefault(require("classnames"));
var _externalContent = require("../../util/external-content");
var _inputText = _interopRequireDefault(require("../../atom/input-text"));
var _button = _interopRequireDefault(require("../../atom/button"));
var _uploadReport = _interopRequireDefault(require("./upload-report.css"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const SuccessMessage = ({
  message
}) => /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("span", {
  className: _uploadReport.default.emoticon
}, "\uD83C\uDF89"), /*#__PURE__*/_react.default.createElement("p", {
  className: _uploadReport.default.message
}, message));
SuccessMessage.propTypes = process.env.NODE_ENV !== "production" ? {
  message: _propTypes.default.string.isRequired
} : {};
const ErrorMessage = ({
  message,
  buttonTitle,
  primaryColor
}) => /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("span", {
  className: _uploadReport.default.emoticon
}, "\uD83E\uDD7A"), /*#__PURE__*/_react.default.createElement("p", {
  className: _uploadReport.default.message
}, message)), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_button.default, {
  "data-name": "cta",
  type: "link",
  submitValue: buttonTitle,
  className: _uploadReport.default.selectButton,
  style: {
    backgroundColor: primaryColor
  }
})));
ErrorMessage.propTypes = process.env.NODE_ENV !== "production" ? {
  message: _propTypes.default.string.isRequired,
  buttonTitle: _propTypes.default.string,
  primaryColor: _propTypes.default.string
} : {};
const UploadReport = ({
  state,
  message,
  warningMessage,
  type: contentType,
  fields,
  mode = 'upload',
  orLabel,
  buttonTitle,
  primaryColor,
  onDelete
}) => {
  const handleDelete = (0, _react.useCallback)(e => {
    e.stopPropagation();
    e.preventDefault();
    onDelete && onDelete(e);
  }, [onDelete]);
  const stopPropagationHandler = (0, _react.useCallback)(e => {
    e.stopPropagation();
    e.preventDefault();
  }, []);
  const showMessage = (0, _includes2.default)(state, ['success', 'error']);
  const fileName = (0, _pipe2.default)(_head2.default, (0, _getOr2.default)('', 'value'), (0, _split2.default)('/'), _last2.default)(fields);
  const fileView = fileName ? /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.fileWrapper
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.fileName,
    title: fileName
  }, fileName), (0, _isEmpty2.default)(warningMessage) ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyValidate, {
    className: _uploadReport.default.validateIcon
  }) : /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.warning
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.showToolTip
  }, /*#__PURE__*/_react.default.createElement("span", null, warningMessage)), /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.warningIcon
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyWarn, {
    width: 11,
    height: 11
  })))) : /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.emptyFileWrapper
  });
  const IconType = _externalContent.EXTERNAL_CONTENT_ICONS[contentType].icon;
  const iconColor = _externalContent.EXTERNAL_CONTENT_ICONS[contentType].color;
  const buildField = (field, index) => /*#__PURE__*/_react.default.createElement(_inputText.default, _extends({}, field, {
    key: index,
    theme: 'cockpit'
  }));
  const deleteView = onDelete ? /*#__PURE__*/_react.default.createElement("div", {
    "data-testid": "trash-icon",
    onClick: handleDelete,
    className: (0, _classnames.default)(_uploadReport.default.actionIcon, _uploadReport.default.trashIcon)
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaLineContentEditionBin, {
    width: 20,
    height: 20
  })) : null;
  const fieldsList = _map2.default.convert({
    cap: false
  })(buildField, fields);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.reportingContainer
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.reportHeader,
    onClick: stopPropagationHandler
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.iconContainer,
    style: {
      backgroundColor: iconColor
    }
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.oval
  }, /*#__PURE__*/_react.default.createElement(IconType, {
    className: _uploadReport.default.iconHeader,
    style: {
      color: iconColor
    }
  }))), fileView, deleteView), /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.reportContainer,
    onClick: stopPropagationHandler
  }, state === 'success' ? /*#__PURE__*/_react.default.createElement(SuccessMessage, {
    message: message
  }) : null, state === 'error' ? /*#__PURE__*/_react.default.createElement(ErrorMessage, {
    message: message,
    buttonTitle: buttonTitle,
    primaryColor: primaryColor
  }) : null, mode === 'edit' && !showMessage ? /*#__PURE__*/_react.default.createElement("div", null, fieldsList) : null, mode === 'edit' && !showMessage ? /*#__PURE__*/_react.default.createElement("span", {
    className: _uploadReport.default.or
  }, orLabel) : null), !showMessage ? /*#__PURE__*/_react.default.createElement("div", {
    className: _uploadReport.default.drop
  }, /*#__PURE__*/_react.default.createElement("p", {
    className: _uploadReport.default.message
  }, message), /*#__PURE__*/_react.default.createElement(_button.default, {
    "data-name": "cta",
    type: "link",
    submitValue: buttonTitle,
    className: _uploadReport.default.selectButton,
    style: {
      backgroundColor: primaryColor
    }
  })) : null);
};
exports.UploadReport = UploadReport;
var _default = exports.default = UploadReport;
UploadReport.propTypes = process.env.NODE_ENV !== "production" ? {
  state: _propTypes.default.string.isRequired,
  message: _propTypes.default.string.isRequired,
  warningMessage: _propTypes.default.string,
  orLabel: _propTypes.default.string,
  type: _propTypes.default.string,
  fields: _propTypes.default.arrayOf(_propTypes.default.shape({
    ..._inputText.default.propTypes,
    type: _propTypes.default.oneOf(['text'])
  })),
  mode: _propTypes.default.oneOf(['edit', 'upload']),
  buttonTitle: _propTypes.default.string,
  primaryColor: _propTypes.default.string,
  onDelete: _propTypes.default.func
} : {};
//# sourceMappingURL=upload-report.js.map