"use strict";

exports.__esModule = true;
exports.default = void 0;
var _omit2 = _interopRequireDefault(require("lodash/fp/omit"));
var _keys2 = _interopRequireDefault(require("lodash/fp/keys"));
var _getOr2 = _interopRequireDefault(require("lodash/fp/getOr"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _isNil2 = _interopRequireDefault(require("lodash/fp/isNil"));
var _defer2 = _interopRequireDefault(require("lodash/fp/defer"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _novaIcons = require("@coorpacademy/nova-icons");
var _classnames = _interopRequireDefault(require("classnames"));
var _loader = _interopRequireDefault(require("../../../atom/loader"));
var _link = _interopRequireDefault(require("../../../atom/link"));
var _provider = _interopRequireDefault(require("../../../atom/provider"));
var _resourceBrowser = _interopRequireDefault(require("../../../organism/resource-browser"));
var _container = _interopRequireDefault(require("../../../organism/accordion/container"));
var _cmPopin = _interopRequireDefault(require("../../../molecule/cm-popin"));
var _popinHeader = _interopRequireDefault(require("../popin-header"));
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 }; }
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 extractTabs = items => (0, _keys2.default)(items).map(type => {
  const item = items[type];
  return {
    iconType: type,
    title: item.title,
    isOpen: item.open
  };
});
const Resources = ({
  resources,
  overlay
}) => /*#__PURE__*/_react.default.createElement("div", {
  className: _style.default.browserWrapper
}, /*#__PURE__*/_react.default.createElement(_resourceBrowser.default, {
  resources: resources.value,
  overlay: overlay,
  className: _style.default.browser
}));
Resources.propTypes = process.env.NODE_ENV !== "production" ? {
  resources: _propTypes.default.shape({
    value: _resourceBrowser.default.propTypes.resources
  }),
  overlay: _resourceBrowser.default.propTypes.overlay
} : {};
const SimpleText = ({
  text
}) => /*#__PURE__*/_react.default.createElement("div", {
  "data-name": "simpleText",
  className: _style.default.simpleTextWrapper
}, /*#__PURE__*/_react.default.createElement("p", {
  className: (0, _classnames.default)(_style.default.simpleText, _style.default.innerHTML)
  // eslint-disable-next-line react/no-danger
  ,
  dangerouslySetInnerHTML: {
    __html: text
  }
}));
SimpleText.propTypes = process.env.NODE_ENV !== "production" ? {
  text: _propTypes.default.string
} : {};
const AssistanceLink = (props, context) => {
  if ((0, _isEmpty2.default)(props)) return null;
  const {
    title,
    onClick
  } = props;
  if ((0, _isEmpty2.default)(title)) return null;
  const {
    skin
  } = context;
  const white = (0, _get2.default)('common.white', skin);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.wrapperAssistance,
    onClick: onClick
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaLineInterfaceFeedbackInterfaceAlertCircle, {
    className: _style.default.alertIcon,
    style: {
      color: white
    }
  }), /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.titleLink
  }, " ", title, " "));
};
AssistanceLink.contextTypes = {
  skin: _provider.default.childContextTypes.skin
};
AssistanceLink.propTypes = process.env.NODE_ENV !== "production" ? {
  title: _propTypes.default.string,
  onClick: _propTypes.default.func
} : {};
const Question = ({
  header,
  answer,
  answerPrefix
}) => /*#__PURE__*/_react.default.createElement("div", {
  className: _style.default.question
}, /*#__PURE__*/_react.default.createElement("p", {
  className: (0, _classnames.default)(_style.default.questionHeader, _style.default.innerHTML)
  // eslint-disable-next-line react/no-danger
  ,
  dangerouslySetInnerHTML: {
    __html: header
  }
}), /*#__PURE__*/_react.default.createElement("div", {
  className: _style.default.answerWrapper
}, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyCheck, {
  className: _style.default.checkIcon
}), answerPrefix ? /*#__PURE__*/_react.default.createElement("span", {
  className: _style.default.answerPrefix
}, answerPrefix) : null, /*#__PURE__*/_react.default.createElement("span", {
  className: (0, _classnames.default)(_style.default.answer, _style.default.innerHTML)
  // eslint-disable-next-line react/no-danger
  ,
  dangerouslySetInnerHTML: {
    __html: answer
  }
})));
Question.propTypes = process.env.NODE_ENV !== "production" ? {
  header: _propTypes.default.string,
  answerPrefix: _propTypes.default.string,
  answer: _propTypes.default.string
} : {};
class PopinCorrection extends _react.Component {
  static contextTypes = {
    skin: _provider.default.childContextTypes.skin
  };
  constructor(props) {
    super(props);
    this.state = {
      open: false
    };
    this.initWrapper = this.initWrapper.bind(this);
  }
  componentDidMount() {
    this.deferOpen();
  }
  deferOpen() {
    clearTimeout(this.deferedOpen);
    this.deferedOpen = (0, _defer2.default)(() => {
      const {
        onOpen
      } = this.props;
      this.setState({
        open: true
      });
      onOpen && onOpen();
    });
  }
  initWrapper(wrapper) {
    this.wrapper = wrapper;
  }
  render() {
    const {
      header = {},
      extraLifeGranted,
      gameOver,
      question,
      overlay,
      resources,
      klf,
      tips,
      onClick,
      quit = {},
      assistanceLink = {},
      popinError
    } = this.props;
    const {
      open
    } = this.state;
    const {
      skin
    } = this.context;
    const primary = (0, _getOr2.default)('#f0f', 'common.primary', skin);
    const tabs = extractTabs({
      resources,
      klf,
      tips
    });
    const isLoading = (0, _isNil2.default)(header.failed);
    const className = open ? _style.default.finalBackground : _style.default.initialBackground;
    const {
      title,
      ...linkProps
    } = quit.cta || {};
    const quitCta = title || extraLifeGranted ? /*#__PURE__*/_react.default.createElement(_link.default, _extends({}, linkProps, {
      style: {
        color: primary
      },
      className: extraLifeGranted ? _style.default.hideQuitCta : _style.default.quitCta,
      "data-name": "nextLink",
      "data-popin": "popinCorrection",
      "data-next": "quit-with-extra-life"
    }), title) : null;
    return /*#__PURE__*/_react.default.createElement("div", {
      ref: this.initWrapper,
      className: className,
      "data-name": "popinCorrection"
    }, popinError ? /*#__PURE__*/_react.default.createElement(_cmPopin.default, popinError) : null, /*#__PURE__*/_react.default.createElement("div", {
      className: _style.default.scrollWrapper
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: isLoading ? _style.default.loadingWrapper : _style.default.wrapper
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: isLoading ? _style.default.loadingContent : _style.default.content
    }, /*#__PURE__*/_react.default.createElement(_popinHeader.default, _extends({}, header, {
      gameOver: gameOver,
      extraLifeGranted: extraLifeGranted,
      animated: true
    })), /*#__PURE__*/_react.default.createElement(Question, question), /*#__PURE__*/_react.default.createElement(_container.default, {
      tabProps: tabs,
      onClick: onClick,
      oneTabOnly: true
    }, (0, _isEmpty2.default)((0, _getOr2.default)([], 'value', resources)) ? null : /*#__PURE__*/_react.default.createElement(Resources, {
      resources: resources,
      overlay: overlay
    }), klf.value ? /*#__PURE__*/_react.default.createElement(SimpleText, {
      text: klf.value
    }) : null, tips.value ? /*#__PURE__*/_react.default.createElement(SimpleText, {
      text: tips.value
    }) : null)), quitCta, /*#__PURE__*/_react.default.createElement(AssistanceLink, assistanceLink)), isLoading ? /*#__PURE__*/_react.default.createElement(_loader.default, {
      className: _style.default.activeLoader
    }) : null));
  }
}
PopinCorrection.propTypes = process.env.NODE_ENV !== "production" ? {
  resources: Resources.propTypes.resources,
  overlay: _resourceBrowser.default.propTypes.overlay,
  assistanceLink: _propTypes.default.shape(AssistanceLink.propTypes),
  header: _propTypes.default.shape((0, _omit2.default)(['animated'], _popinHeader.default.propTypes)),
  extraLifeGranted: _popinHeader.default.propTypes.extraLifeGranted,
  gameOver: _popinHeader.default.propTypes.gameOver,
  question: _propTypes.default.shape(Question.propTypes),
  klf: _propTypes.default.shape(SimpleText.propTypes),
  tips: _propTypes.default.shape(SimpleText.propTypes),
  onClick: _container.default.propTypes.onClick,
  quit: _propTypes.default.shape({
    cta: _propTypes.default.shape({
      ..._link.default.propTypes,
      title: _propTypes.default.string
    })
  }),
  onOpen: _propTypes.default.func,
  popinError: _propTypes.default.shape(_cmPopin.default.propTypes)
} : {};
var _default = exports.default = PopinCorrection;
//# sourceMappingURL=index.js.map