"use strict";

exports.__esModule = true;
exports.default = void 0;
var _isEmpty2 = _interopRequireDefault(require("lodash/fp/isEmpty"));
var _multiply2 = _interopRequireDefault(require("lodash/fp/multiply"));
var _parseInt3 = _interopRequireDefault(require("lodash/fp/parseInt"));
var _round2 = _interopRequireDefault(require("lodash/fp/round"));
var _zip2 = _interopRequireDefault(require("lodash/fp/zip"));
var _times2 = _interopRequireDefault(require("lodash/fp/times"));
var _pipe2 = _interopRequireDefault(require("lodash/fp/pipe"));
var _isNil2 = _interopRequireDefault(require("lodash/fp/isNil"));
var _get2 = _interopRequireDefault(require("lodash/fp/get"));
var _flatten2 = _interopRequireDefault(require("lodash/fp/flatten"));
var _compact2 = _interopRequireDefault(require("lodash/fp/compact"));
var _react = _interopRequireDefault(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 _life = _interopRequireDefault(require("../../../atom/life"));
var _link = _interopRequireDefault(require("../../../atom/link"));
var _animation = _interopRequireWildcard(require("../../../hoc/animation"));
var _transition = _interopRequireDefault(require("../../../hoc/transition"));
var _animationScheduler = _interopRequireWildcard(require("../../../hoc/animation-scheduler"));
var _provider = _interopRequireDefault(require("../../../atom/provider"));
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 separator = index => /*#__PURE__*/_react.default.createElement("span", {
  key: index,
  className: _style.default.answerSeparator
}, "/");
const joinBySeparator = elements => {
  const separators = (0, _times2.default)(separator, elements.length - 1);
  return (0, _pipe2.default)(_zip2.default, _flatten2.default, _compact2.default)(elements, separators);
};
const AnswersCorrection = ({
  corrections
}) => {
  const answers = corrections.map((correction, index) => {
    const key = `answer_${index}`;
    return /*#__PURE__*/_react.default.createElement("span", {
      key: key,
      "data-name": "answer",
      "data-correct": correction.isCorrect,
      className: (0, _classnames.default)(correction.isCorrect ? _style.default.correctAnswer : _style.default.wrongAnswer, _style.default.innerHTML)
      // eslint-disable-next-line react/no-danger
      ,
      dangerouslySetInnerHTML: {
        __html: correction.answer
      }
    });
  }, corrections);
  return /*#__PURE__*/_react.default.createElement("p", {
    className: _style.default.fullAnswer
  }, joinBySeparator(answers));
};
AnswersCorrection.propTypes = process.env.NODE_ENV !== "production" ? {
  corrections: _propTypes.default.arrayOf(_propTypes.default.shape({
    answer: _propTypes.default.string.isRequired,
    isCorrect: _propTypes.default.bool.isRequired
  }))
} : {};
const formatPlusSign = value => (value >= 0 ? '+' : '') + value;
const Rank = ({
  failed,
  rank,
  animated,
  onAnimationEnd
}, {
  skin
}) => {
  const positive = (0, _get2.default)('common.positive', skin);
  if (failed || (0, _isNil2.default)(rank)) return null;
  return /*#__PURE__*/_react.default.createElement(_animationScheduler.default, {
    animated: animated,
    onAnimationEnd: onAnimationEnd
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.centerContent
  }, /*#__PURE__*/_react.default.createElement(_transition.default, {
    name: "label",
    after: "counter",
    className: _style.default.bumped
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.iconBubble
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyCharts, {
    className: _style.default.icon,
    style: {
      color: positive
    }
  }))), /*#__PURE__*/_react.default.createElement("span", {
    className: _style.default.iconText
  }, /*#__PURE__*/_react.default.createElement(_animation.default, {
    name: "counter",
    bezier: _animation.EASE_OUT_CUBIC,
    duration: 1000
  }, progress => (0, _pipe2.default)((0, _parseInt3.default)(10), (0, _multiply2.default)(progress), _round2.default, formatPlusSign)(rank)))));
};
Rank.propTypes = process.env.NODE_ENV !== "production" ? {
  failed: _propTypes.default.bool,
  rank: _propTypes.default.string,
  animated: _animation.default.propTypes.animated,
  onAnimationEnd: _animation.default.propTypes.onAnimationEnd
} : {};
Rank.contextTypes = {
  skin: _provider.default.childContextTypes.skin
};
const Stars = ({
  failed,
  stars,
  animated,
  onAnimationEnd
}, {
  skin
}) => {
  const positive = (0, _get2.default)('common.positive', skin);
  if (failed || (0, _isNil2.default)(stars)) return null;
  return /*#__PURE__*/_react.default.createElement(_animationScheduler.default, {
    animated: animated,
    onAnimationEnd: onAnimationEnd
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.centerContent
  }, /*#__PURE__*/_react.default.createElement(_transition.default, {
    name: "label",
    after: "counter",
    className: _style.default.bumped
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.iconBubble
  }, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyStar, {
    className: _style.default.icon,
    style: {
      color: positive
    }
  }))), /*#__PURE__*/_react.default.createElement("span", {
    "data-name": "iconText",
    className: _style.default.iconText
  }, /*#__PURE__*/_react.default.createElement(_animation.default, {
    name: "counter",
    bezier: _animation.EASE_OUT_CUBIC,
    duration: 1000
  }, progress => (0, _pipe2.default)((0, _parseInt3.default)(10), (0, _multiply2.default)(progress), _round2.default, formatPlusSign)(stars)))));
};
Stars.propTypes = process.env.NODE_ENV !== "production" ? {
  failed: _propTypes.default.bool,
  stars: _propTypes.default.string,
  animated: _animation.default.propTypes.animated,
  onAnimationEnd: _animation.default.propTypes.onAnimationEnd
} : {};
Stars.contextTypes = {
  skin: _provider.default.childContextTypes.skin
};
const Lifes = ({
  lives,
  failed,
  animated,
  revival
}) => {
  if ((0, _isNil2.default)(lives)) return null;
  return /*#__PURE__*/_react.default.createElement(_life.default, {
    failed: failed,
    count: lives,
    animated: animated,
    revival: revival,
    className: _style.default.life
  });
};
Lifes.propTypes = process.env.NODE_ENV !== "production" ? {
  lives: _life.default.propTypes.count,
  failed: _life.default.propTypes.failed,
  animated: _life.default.propTypes.animated,
  revival: _life.default.propTypes.revival
} : {};
const IconsPart = props => {
  const {
    lives,
    failed,
    stars,
    rank,
    animated,
    revival
  } = props;
  return /*#__PURE__*/_react.default.createElement(_animationScheduler.default, {
    animated: true
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.iconsWrapper
  }, /*#__PURE__*/_react.default.createElement(Lifes, {
    lives: lives,
    failed: failed,
    animated: animated,
    revival: revival
  }), /*#__PURE__*/_react.default.createElement(_animationScheduler.AnimationAdapter, {
    name: "stars"
  }, /*#__PURE__*/_react.default.createElement(Stars, {
    stars: stars,
    fail: failed
  })), /*#__PURE__*/_react.default.createElement(_animationScheduler.AnimationAdapter, {
    name: "rank",
    after: "stars"
  }, /*#__PURE__*/_react.default.createElement(Rank, {
    rank: rank,
    fail: failed
  }))));
};
IconsPart.propTypes = process.env.NODE_ENV !== "production" ? {
  lives: Lifes.propTypes.lives,
  failed: Lifes.propTypes.failed,
  stars: Stars.propTypes.stars,
  rank: Rank.propTypes.rank,
  animated: _animationScheduler.default.propTypes.animated,
  revival: _propTypes.default.bool
} : {};
const buildClass = (value, success, failed, loading, scormStyle, mode = 'default') => {
  if (loading && (0, _isNil2.default)(value)) return loading;
  if (mode === 'scorm') return scormStyle;
  return value ? failed : success;
};
const getLinkStyle = ({
  gameOver,
  extraLifeGranted
}) => {
  if (extraLifeGranted) {
    return _style.default.oneMoreLife;
  } else if (gameOver) {
    return _style.default.gameOver;
  } else {
    return null;
  }
};
const renderIconStatusScorm = failed => {
  return /*#__PURE__*/_react.default.createElement("div", {
    className: failed ? _style.default.iconCloseContainer : _style.default.iconSuccessContainer
  }, failed ? /*#__PURE__*/_react.default.createElement(_novaIcons.NovaSolidStatusClose, {
    className: _style.default.failIcon
  }) : /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionCoorpacademyCheck, {
    className: _style.default.checkIcon
  }));
};
const buildDefaultOrScormStyle = (defaultStyle, styleScorm, failed, mode = 'default', failedStyle = null) => {
  if (mode === 'scorm' && failed && failedStyle) return failedStyle;
  if (mode === 'scorm') return styleScorm;
  return defaultStyle;
};
const CorrectionPart = props => {
  const {
    failed,
    corrections,
    title,
    subtitle,
    stars,
    rank,
    gameOver,
    mode = 'default'
  } = props;
  const isLoading = (0, _isNil2.default)(failed);
  const className = buildClass(failed, stars && rank ? _style.default.correctionSectionEndSuccess : _style.default.correctionSectionSuccess, gameOver ? _style.default.correctionSectionFailGameOver : _style.default.correctionSectionFail, _style.default.correctionSectionLoading, _style.default.resultContenantContainerScorm, mode);
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-name": "correctionSection",
    className: className
  }, mode === 'scorm' && !isLoading && renderIconStatusScorm(failed), /*#__PURE__*/_react.default.createElement("div", {
    className: buildDefaultOrScormStyle(_style.default.titlesWrapper, _style.default.titlesWrapperScorm, failed, mode)
  }, isLoading ? /*#__PURE__*/_react.default.createElement(_loader.default, null) : null, /*#__PURE__*/_react.default.createElement("h1", {
    "data-name": "title",
    className: buildDefaultOrScormStyle((0, _classnames.default)(_style.default.title, _style.default.innerHTML), _style.default.resultTitleScorm, failed, mode)
    // eslint-disable-next-line react/no-danger
    ,
    dangerouslySetInnerHTML: {
      __html: title
    }
  }), /*#__PURE__*/_react.default.createElement("h2", {
    className: buildDefaultOrScormStyle(_style.default.subtitle, _style.default.resultSubtitleScorm, failed, mode, _style.default.statusPlayerResultFailScorm),
    role: 'status'
  }, subtitle), failed && !(0, _isEmpty2.default)(corrections) ? /*#__PURE__*/_react.default.createElement(AnswersCorrection, {
    corrections: corrections
  }) : null), mode !== 'scorm' ? /*#__PURE__*/_react.default.createElement(IconsPart, props) : null);
};
CorrectionPart.propTypes = process.env.NODE_ENV !== "production" ? {
  ...IconsPart.propTypes,
  failed: _propTypes.default.bool,
  corrections: AnswersCorrection.propTypes.corrections,
  title: _propTypes.default.string,
  subtitle: _propTypes.default.string,
  stars: _propTypes.default.string,
  rank: _propTypes.default.string,
  gameOver: _propTypes.default.bool
} : {};
const NextQuestionPart = (props, context) => {
  const {
    cta,
    type,
    extraLifeGranted,
    gameOver,
    failed = false,
    lives = 0,
    mode
  } = props;
  const {
    title,
    nextStepTitle,
    showNextLevel = false,
    ...linkProps
  } = cta || {};
  let dataNext;
  switch (type) {
    case 'popinCorrection':
      {
        if (gameOver) {
          dataNext = 'game-over';
        } else if (failed) {
          if (lives > 0) {
            dataNext = 'continue-failure';
          } else if (extraLifeGranted) {
            dataNext = 'continue-used-extra-life';
          }
        } else {
          dataNext = 'continue-success';
        }
        break;
      }
    case 'popinEnd':
      {
        if (failed) {
          dataNext = 'redo-content';
        } else if (showNextLevel) {
          dataNext = 'next-level';
        } else {
          dataNext = 'home';
        }
      }
  }
  const nextStep = nextStepTitle ? /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(gameOver ? _style.default.gameOverSubtitle : _style.default.nextStepTitle, _style.default.innerHTML)
    // eslint-disable-next-line react/no-danger
    ,
    dangerouslySetInnerHTML: {
      __html: nextStepTitle
    }
  }) : null;
  return mode !== 'scorm' ? /*#__PURE__*/_react.default.createElement(_link.default, _extends({}, linkProps, {
    className: (0, _classnames.default)(_style.default.nextSection, getLinkStyle({
      gameOver,
      extraLifeGranted
    })),
    "data-name": "nextLink",
    "data-popin": type,
    "data-next": dataNext
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.wrapperNextSection
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.nextButton
  }, title, /*#__PURE__*/_react.default.createElement(_novaIcons.NovaCompositionNavigationArrowRight, {
    className: _style.default.nextButtonIcon
  })), nextStep)) : null;
};
NextQuestionPart.propTypes = process.env.NODE_ENV !== "production" ? {
  cta: _propTypes.default.shape({
    ..._link.default.propTypes,
    title: _propTypes.default.string,
    nextStepTitle: _propTypes.default.string,
    showNextLevel: _propTypes.default.bool
  }),
  type: _propTypes.default.oneOf(['popinCorrection', 'popinEnd']).isRequired,
  extraLifeGranted: _propTypes.default.bool,
  gameOver: _propTypes.default.bool,
  failed: _propTypes.default.bool,
  lives: _propTypes.default.number,
  mode: _propTypes.default.string
} : {};
const PopinHeader = (props, context) => {
  const {
    animated,
    failed,
    title,
    subtitle,
    lives,
    stars,
    rank,
    corrections,
    cta,
    extraLifeGranted,
    gameOver = false,
    type,
    mode = 'default'
  } = props;
  const state = buildClass(failed, 'success', 'failed', null);
  const nextLink = cta ? /*#__PURE__*/_react.default.createElement(NextQuestionPart, {
    cta: cta,
    type: type,
    extraLifeGranted: extraLifeGranted,
    failed: failed,
    gameOver: gameOver,
    lives: lives,
    mode: mode
  }) : null;
  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(mode === 'scorm' && _style.default.scorm, cta ? mode !== 'scorm' && _style.default.header : mode !== 'scorm' && _style.default.headerWithoutCTA),
    "data-name": "popinHeader",
    "data-state": state
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(mode !== 'scorm' && _style.default.headerTitle)
  }, /*#__PURE__*/_react.default.createElement(CorrectionPart, {
    title: title,
    subtitle: subtitle,
    lives: lives,
    animated: animated,
    stars: stars,
    rank: rank,
    failed: failed,
    gameOver: gameOver,
    revival: extraLifeGranted,
    corrections: corrections,
    mode: mode
  }), nextLink));
};
PopinHeader.contextTypes = {
  skin: _provider.default.childContextTypes.skin
};
PopinHeader.propTypes = process.env.NODE_ENV !== "production" ? {
  cta: NextQuestionPart.propTypes.cta,
  type: NextQuestionPart.propTypes.type,
  extraLifeGranted: NextQuestionPart.propTypes.extraLifeGranted,
  failed: NextQuestionPart.propTypes.failed,
  gameOver: NextQuestionPart.propTypes.gameOver,
  lives: NextQuestionPart.propTypes.lives,
  title: CorrectionPart.propTypes.title,
  subtitle: CorrectionPart.propTypes.subtitle,
  animated: CorrectionPart.propTypes.animated,
  stars: CorrectionPart.propTypes.stars,
  rank: CorrectionPart.propTypes.rank,
  corrections: CorrectionPart.propTypes.corrections,
  mode: _propTypes.default.oneOf(['scorm', 'default'])
} : {};
var _default = exports.default = PopinHeader;
//# sourceMappingURL=index.js.map