import _get from "lodash/fp/get";
import _find from "lodash/fp/find";
import _pipe from "lodash/fp/pipe";
import _size from "lodash/fp/size";
import _map from "lodash/fp/map";
import _isEmpty from "lodash/fp/isEmpty";
import _noop from "lodash/fp/noop";
import _keys from "lodash/fp/keys";
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { COLORS } from '../../variables/colors';
import ButtonLink from '../../atom/button-link';
import Icon from '../../atom/icon';
import InputSwitch from '../../atom/input-switch';
import style from './style.css';
const uncappedMap = _map.convert({
  cap: false
});
const TYPES = {
  success: ['circle-check', COLORS.cm_positive_200],
  error: ['triangle-exclamation', COLORS.cm_negative_200],
  warning: ['circle-exclamation', COLORS.cm_yellow_400],
  info: ['circle-info', COLORS.cm_grey_500]
};
const STYLES = {
  success: style.success,
  error: style.error,
  warning: style.warning,
  info: style.info
};
const Banner = props => {
  const {
    type,
    message,
    cta = [],
    temporary,
    bannerKey,
    onEnd
  } = props;
  const oldSwitchValue = _pipe(_find({
    type: 'switch'
  }), _get('oldSwitchValue'))(cta);
  const [switchValue, setSwitchValue] = useState(oldSwitchValue || false);
  const ButtonSeparator = /*#__PURE__*/React.createElement("div", {
    className: classnames(style.buttonsBar, STYLES[type])
  });
  const handleSwitchToggle = useCallback(action => e => {
    const newSwitchValue = !switchValue;
    setSwitchValue(newSwitchValue);
    action(newSwitchValue);
  }, [switchValue]);
  const buildButton = ({
    type: buttonType = 'button',
    label,
    action = _noop
  }) => {
    const commonStyle = classnames(style.button, STYLES[type]);
    switch (buttonType) {
      case 'switch':
        return /*#__PURE__*/React.createElement("div", {
          className: commonStyle
        }, /*#__PURE__*/React.createElement(InputSwitch, {
          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.createElement("div", {
          className: commonStyle
        }, /*#__PURE__*/React.createElement(ButtonLink, {
          "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 (_isEmpty(options)) return null;
      return /*#__PURE__*/React.createElement("div", {
        className: style.ctaWrapper,
        key: i
      }, i > 0 && i <= _size(options) ? ButtonSeparator : null, buildButton(options));
    }, ctaOptions);
  };
  const [iconName, iconColor] = TYPES[type];
  return /*#__PURE__*/React.createElement("div", {
    key: bannerKey,
    className: classnames(style.banner, STYLES[type], temporary && style.temporaryBanner),
    onAnimationEnd: onEnd,
    title: message
  }, /*#__PURE__*/React.createElement("div", {
    "data-name": `${type}-banner-message`,
    className: style.message
  }, /*#__PURE__*/React.createElement(Icon, {
    iconName: iconName,
    iconColor: iconColor,
    className: classnames(style.icon, temporary && style.temporaryIcon),
    customStyle: {
      padding: 0
    }
  }), message), buildCta(cta));
};
const ctaPropTypes = process.env.NODE_ENV !== "production" ? PropTypes.shape({
  label: PropTypes.string,
  type: PropTypes.oneOf(['button', 'switch']),
  action: PropTypes.func,
  oldSwitchValue: PropTypes.bool
}) : {};
Banner.propTypes = process.env.NODE_ENV !== "production" ? {
  bannerKey: PropTypes.string,
  type: PropTypes.oneOf(_keys(TYPES)),
  message: PropTypes.string.isRequired,
  cta: PropTypes.arrayOf(ctaPropTypes),
  temporary: PropTypes.bool,
  onEnd: PropTypes.func
} : {};
export default Banner;
//# sourceMappingURL=index.js.map