"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _map = _interopRequireDefault(require("lodash/fp/map"));
var _isEmpty = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _classnames = _interopRequireDefault(require("classnames"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _buttonLink = _interopRequireDefault(require("../../atom/button-link"));
var _buttonLinkIcon = _interopRequireDefault(require("../../atom/button-link-icon"));
var _inputSwitch = _interopRequireDefault(require("../../atom/input-switch"));
var _title = _interopRequireDefault(require("../../atom/title"));
var _cardsGrid = _interopRequireDefault(require("../../organism/cards-grid"));
var _listItems = _interopRequireDefault(require("../../organism/list-items"));
var _link = _interopRequireDefault(require("../../atom/link"));
var _icon = _interopRequireDefault(require("../../atom/icon"));
var _style = _interopRequireDefault(require("./style.css"));
var _types = _interopRequireDefault(require("./types"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 _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); }
// DEPRECATED
const LOGO = {
  AlertDiamond: _novaIcons.NovaSolidInterfaceFeedbackInterfaceAlertDiamond,
  WindowUpload: _novaIcons.NovaSolidApplicationsWindowUpload3,
  MoonRocket: _novaIcons.NovaSolidSpaceMoonRocket,
  CheckCircle1: _novaIcons.NovaLineStatusCheckCircle1,
  AlertIcon: _novaIcons.NovaSolidInterfaceFeedbackInterfaceAlertRounded
};
const IconType = {
  lockedContent: /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyPadlock, {
    className: _style.default.lockIcon
  }),
  fileZipped: /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.filesListIconContainer
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidFilesBasicFileLines, {
    className: _style.default.filesListIcon
  }))
};
const CMPopin = props => {
  const {
    content,
    mode = 'alert',
    firstButton,
    secondButton,
    onClose,
    header,
    icon,
    backgroundImageUrl,
    descriptionText,
    thirdButton,
    cookieTitle,
    descriptionBtnTxt,
    listBtnSwicth,
    items,
    link
  } = props;
  const backgroundImageStyle = backgroundImageUrl ? {
    backgroundImage: `url(${backgroundImageUrl})`,
    backgroundSize: 'cover'
  } : null;
  const handleCloseButton = (0, _react.useMemo)(() => () => onClose(), [onClose]);
  const nodeRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (mode === 'items') {
      const closePopin = e => {
        if (nodeRef && nodeRef.current && !nodeRef.current.contains(e.target)) {
          handleCloseButton();
        }
      };
      const handleKeyDown = e => {
        if (e?.key === 'Escape') {
          handleCloseButton();
        }
      };
      document.addEventListener('click', closePopin);
      document.addEventListener('touchstart', closePopin);
      document.addEventListener('keydown', handleKeyDown);
      return () => {
        document.removeEventListener('click', closePopin);
        document.removeEventListener('touchstart', closePopin);
        document.removeEventListener('keydown', handleKeyDown);
      };
    }
  }, [handleCloseButton, mode, onClose]);
  const wrapperPopinStyle = (0, _classnames.default)(_style.default.popin, mode === 'cookie' && _style.default.popinCookie, mode === 'list' && _style.default.popinFilesList, mode === 'items' && _style.default.popinItems);
  const buildIcon = () => {
    if (typeof icon === 'string') {
      const LogoComponent = LOGO[icon];
      return LogoComponent ? /*#__PURE__*/_react.default.createElement(LogoComponent, {
        className: _style.default.icon
      }) : null;
    }
    return !(0, _isEmpty.default)(icon) ? /*#__PURE__*/_react.default.createElement(_icon.default, _extends({}, icon, {
      className: _style.default.icon
    })) : null;
  };
  const buildHeader = () => {
    if (header) {
      const {
        title,
        headerIcon,
        backgroundImage
      } = header;
      const TopTitleIcon = IconType[headerIcon];
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, title ? /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.headerContent
      }, TopTitleIcon, /*#__PURE__*/_react.default.createElement(_title.default, title)) : null, backgroundImage ? /*#__PURE__*/_react.default.createElement("img", {
        className: _style.default.headerBackground,
        src: backgroundImage
      }) : null);
    }
    if (mode === 'cookie') return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.cookieHeader
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.cookieIconContainer
    }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaLineSettingsCookie, {
      className: _style.default.cookieIcon
    })), /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.cookieTitle
    }, cookieTitle));
  };
  const renderHeader = () => {
    const headerContent = buildHeader();
    const close = onClose ? /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.onCloseButton
    }, /*#__PURE__*/_react.default.createElement(_buttonLinkIcon.default, {
      onClick: handleCloseButton,
      "data-name": 'close-icon',
      "aria-label": 'close-icon',
      size: "small",
      icon: "close"
    })) : null;
    if ((0, _isEmpty.default)(headerContent) && (0, _isEmpty.default)(close)) return null;
    return /*#__PURE__*/_react.default.createElement("header", {
      className: _style.default.popinHeader
    }, headerContent, close);
  };
  const getClassBtnSwitch = (index, btnList) => {
    switch (index) {
      case 0:
        return _style.default.firstBtnSwitchContainer;
      case btnList.length - 1:
        return _style.default.lastBtnSwitchContainer;
      default:
        return _style.default.singleSwitchContainer;
    }
  };
  const renderBtnSwitch = () => {
    return _map.default.convert({
      cap: false
    })((el, index) => {
      const {
        type,
        title,
        value,
        onChange,
        titlePosition,
        theme,
        details,
        requiredSelection,
        disabled,
        id,
        'data-name': dataName
      } = el;
      return /*#__PURE__*/_react.default.createElement("div", {
        key: index,
        className: getClassBtnSwitch(index, listBtnSwicth)
      }, /*#__PURE__*/_react.default.createElement(_inputSwitch.default, {
        type,
        title,
        value,
        onChange,
        titlePosition,
        theme,
        details,
        requiredSelection,
        disabled,
        id,
        'data-name': dataName
      }));
    })(listBtnSwicth);
  };
  const renderItems = () => {
    if ((0, _isEmpty.default)(items)) return null;
    const {
      type,
      list
    } = items;
    if (type === 'content') return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.itemsContainer,
      "data-name": 'cm-popin-cards'
    }, /*#__PURE__*/_react.default.createElement(_cardsGrid.default, list));
    if (type === 'list') return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.filesListContainer
    }, /*#__PURE__*/_react.default.createElement(_listItems.default, list));
    return null;
  };
  const renderContent = () => {
    if (mode === 'items' || mode === 'list') return;
    const isCookieOrInformations = mode === 'cookie' || mode === 'information';
    const renderContentTitle = () => {
      if (!content) return null;
      if (isCookieOrInformations) return /*#__PURE__*/_react.default.createElement("p", {
        className: mode === 'alert' ? _style.default.content : _style.default.message,
        "data-name": 'cm-popin-content'
        // eslint-disable-next-line react/no-danger
        ,
        dangerouslySetInnerHTML: {
          __html: content
        }
      });
      return /*#__PURE__*/_react.default.createElement("div", {
        className: mode === 'alert' ? _style.default.content : _style.default.message
      }, /*#__PURE__*/_react.default.createElement(_title.default, {
        title: content,
        "data-name": 'cm-popin-content'
      }));
    };
    return /*#__PURE__*/_react.default.createElement("div", {
      className: isCookieOrInformations ? _style.default.cookieTitleContainer : _style.default.titleContainer
    }, buildIcon(), /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.contentSection
    }, renderContentTitle(), descriptionText ? /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.descriptionText
    }, descriptionText) : null), link ? /*#__PURE__*/_react.default.createElement(_link.default, link) : null);
  };
  const renderButtons = () => {
    if (!firstButton && !secondButton && !thirdButton) return null;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.buttonContainer
    }, firstButton ? /*#__PURE__*/_react.default.createElement("div", {
      className: firstButton.largeButton ? _style.default.largeButton : _style.default.button
    }, /*#__PURE__*/_react.default.createElement(_buttonLink.default, _extends({}, firstButton, {
      onClick: firstButton.handleOnclick,
      "data-name": 'cm-popin-cta'
    }))) : null, secondButton ? /*#__PURE__*/_react.default.createElement("div", {
      className: secondButton.largeButton ? _style.default.largeButton : _style.default.button
    }, /*#__PURE__*/_react.default.createElement(_buttonLink.default, _extends({}, secondButton, {
      onClick: secondButton.handleOnclick,
      "data-name": `cm-popin-cta-${secondButton.type}`
    }))) : null, thirdButton ? /*#__PURE__*/_react.default.createElement("div", {
      className: thirdButton.largeButton ? _style.default.largeButton : _style.default.button
    }, /*#__PURE__*/_react.default.createElement(_buttonLink.default, _extends({}, thirdButton, {
      onClick: thirdButton.handleOnclick,
      "data-name": `cm-popin-cta-${thirdButton.type}`
    }))) : null);
  };
  return /*#__PURE__*/_react.default.createElement("div", {
    className: mode !== 'cookie' ? _style.default.background : null,
    style: backgroundImageStyle,
    "data-name": 'cm-popin-container'
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: wrapperPopinStyle,
    ref: nodeRef
  }, renderHeader(), renderContent(), descriptionBtnTxt ? /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.descriptionBtn
  }, descriptionBtnTxt) : null, renderItems(), renderBtnSwitch(), renderButtons()));
};
CMPopin.propTypes = process.env.NODE_ENV !== "production" ? _types.default : {};
CMPopin.contextTypes = {
  translate: _provider.default.childContextTypes.translate
};
var _default = exports.default = CMPopin;
//# sourceMappingURL=index.js.map