"use strict";

exports.__esModule = true;
exports.default = void 0;
var _find2 = _interopRequireDefault(require("lodash/fp/find"));
var _map2 = _interopRequireDefault(require("lodash/fp/map"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _parseTemplateString = _interopRequireDefault(require("../../../util/parse-template-string"));
var _dropDown = _interopRequireDefault(require("../drop-down"));
var _freeText = _interopRequireDefault(require("../free-text"));
var _style = _interopRequireDefault(require("./style.css"));
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 Template = ({
  template,
  answers,
  groupAriaLabel
}) => {
  const totalTemplate = (0, _parseTemplateString.default)(template);
  const templateCompose = _map2.default.convert({
    cap: false
  })((part, key) => {
    const type = part.type;
    if (type === 'string') {
      return /*#__PURE__*/_react.default.createElement("span", {
        key: key,
        className: _style.default.textPart
        // eslint-disable-next-line react/no-danger
        ,
        dangerouslySetInnerHTML: {
          __html: part.value
        }
      });
    }
    if (type === 'answerField') {
      const field = (0, _find2.default)({
        name: part.value
      }, answers);
      if (!field) return null;
      const {
        type: fieldType,
        ...fieldProps
      } = field;
      const fieldView = fieldType === 'text' ? /*#__PURE__*/_react.default.createElement(_freeText.default, _extends({}, fieldProps, {
        className: _style.default.text
      })) : /*#__PURE__*/_react.default.createElement(_dropDown.default, _extends({}, fieldProps, {
        theme: "player"
      }));
      return /*#__PURE__*/_react.default.createElement("div", {
        className: _style.default.answerType,
        key: part.value
      }, fieldView);
    }
  }, totalTemplate);
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-name": "template-wrapper",
    className: _style.default.wrapper,
    role: "group",
    "aria-label": groupAriaLabel,
    title: groupAriaLabel
  }, templateCompose);
};
const TextPropTypes = process.env.NODE_ENV !== "production" ? {
  ..._dropDown.default.propTypes,
  type: _propTypes.default.string
} : {};
Template.propTypes = process.env.NODE_ENV !== "production" ? {
  template: _propTypes.default.string,
  answers: _propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.shape(_dropDown.default.propTypes), _propTypes.default.shape(TextPropTypes)])),
  groupAriaLabel: _propTypes.default.string
} : {};
var _default = exports.default = Template;
//# sourceMappingURL=index.js.map