"use strict";

exports.__esModule = true;
exports.default = void 0;
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _find2 = _interopRequireDefault(require("lodash/fp/find"));
var _pipe2 = _interopRequireDefault(require("lodash/fp/pipe"));
var _size2 = _interopRequireDefault(require("lodash/fp/size"));
var _map2 = _interopRequireDefault(require("lodash/fp/map"));
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _noop2 = _interopRequireDefault(require("lodash/fp/noop"));
var _keys2 = _interopRequireDefault(require("lodash/fp/keys"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _colors = require("../../variables/colors");
var _buttonLink = _interopRequireDefault(require("../../atom/button-link"));
var _icon = _interopRequireDefault(require("../../atom/icon"));
var _inputSwitch = _interopRequireDefault(require("../../atom/input-switch"));
var _style = _interopRequireDefault(require("./style.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 }; }
const uncappedMap = _map2.default.convert({
  cap: false
});
const TYPES = {
  success: ['circle-check', _colors.COLORS.cm_positive_200],
  error: ['triangle-exclamation', _colors.COLORS.cm_negative_200],
  warning: ['circle-exclamation', _colors.COLORS.cm_yellow_400],
  info: ['circle-info', _colors.COLORS.cm_grey_500]
};
const STYLES = {
  success: _style.default.success,
  error: _style.default.error,
  warning: _style.default.warning,
  info: _style.default.info
};
const Banner = props => {
  const {
    type,
    message,
    cta = [],
    temporary,
    bannerKey,
    onEnd
  } = props;
  const oldSwitchValue = (0, _pipe2.default)((0, _find2.default)({
    type: 'switch'
  }), (0, _get2.default)('oldSwitchValue'))(cta);
  const [switchValue, setSwitchValue] = (0, _react.useState)(oldSwitchValue || false);
  const ButtonSeparator = /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(_style.default.buttonsBar, STYLES[type])
  });
  const handleSwitchToggle = (0, _react.useCallback)(action => e => {
    const newSwitchValue = !switchValue;
    setSwitchValue(newSwitchValue);
    action(newSwitchValue);
  }, [switchValue]);
  const buildButton = ({
    type: buttonType = 'button',
    label,
    action = _noop2.default
  }) => {
    const commonStyle = (0, _classnames.default)(_style.default.button, STYLES[type]);
    switch (buttonType) {
      case 'switch':
        return /*#__PURE__*/_react.default.createElement("div", {
          className: commonStyle
        }, /*#__PURE__*/_react.default.createElement(_inputSwitch.default, {
          id: "show-microlearning-switch",
          theme: "default",
          title: label,
          value: switchValue,
          onChange: handleSwitchToggle(action),
          "data-name": `banner-switch-cta`,
          "aria-label": label
        }));
      default:
        return /*#__PURE__*/_react.default.createElement("div", {
          className: commonStyle
        }, /*#__PURE__*/_react.default.createElement(_buttonLink.default, {
          "data-name": "banner-button-cta",
          "aria-label": label,
          label: label,
          onClick: action,
          type: "text",
          customStyle: {
            padding: 0
          }
        }));
    }
  };
  const buildCta = ctaOptions => {
    return uncappedMap((options, i) => {
      if ((0, _isEmpty2.default)(options)) return null;
      return /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.ctaWrapper,
        key: i
      }, i > 0 && i <= (0, _size2.default)(options) ? ButtonSeparator : null, buildButton(options));
    }, ctaOptions);
  };
  const [iconName, iconColor] = TYPES[type];
  return /*#__PURE__*/_react.default.createElement("div", {
    key: bannerKey,
    className: (0, _classnames.default)(_style.default.banner, STYLES[type], temporary && _style.default.temporaryBanner),
    onAnimationEnd: onEnd,
    title: message
  }, /*#__PURE__*/_react.default.createElement("div", {
    "data-name": `${type}-banner-message`,
    className: _style.default.message
  }, /*#__PURE__*/_react.default.createElement(_icon.default, {
    iconName: iconName,
    iconColor: iconColor,
    className: (0, _classnames.default)(_style.default.icon, temporary && _style.default.temporaryIcon),
    customStyle: {
      padding: 0
    }
  }), message), buildCta(cta));
};
const ctaPropTypes = process.env.NODE_ENV !== "production" ? _propTypes.default.shape({
  label: _propTypes.default.string,
  type: _propTypes.default.oneOf(['button', 'switch']),
  action: _propTypes.default.func,
  oldSwitchValue: _propTypes.default.bool
}) : {};
Banner.propTypes = process.env.NODE_ENV !== "production" ? {
  bannerKey: _propTypes.default.string,
  type: _propTypes.default.oneOf((0, _keys2.default)(TYPES)),
  message: _propTypes.default.string.isRequired,
  cta: _propTypes.default.arrayOf(ctaPropTypes),
  temporary: _propTypes.default.bool,
  onEnd: _propTypes.default.func
} : {};
var _default = exports.default = Banner;
//# sourceMappingURL=index.js.map