"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _wrapRegExp2 = _interopRequireDefault(require("@babel/runtime/helpers/wrapRegExp"));

var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));

var _base64Js = require("base64-js");

var _dateFns = require("date-fns");

var _lodash = require("lodash");

var _prettyBytes = _interopRequireDefault(require("pretty-bytes"));

var _reactIntl = require("react-intl");

var _ = require("..");

var _stepTitle = require("../../configToSchemas/types/stepTitle");

var _title = require("../../configToSchemas/types/title");

var _Markdown = _interopRequireDefault(require("../../Markdown"));

var _styles = require("@material-ui/core/styles");

var _jsxRuntime = require("react/jsx-runtime");

var useStyles = (0, _styles.makeStyles)({
  image: {
    maxWidth: '100%',
    width: 'fit-content'
  }
});

var dataUrlByteLength = function dataUrlByteLength(dataUrl) {
  return (0, _base64Js.byteLength)(dataUrl.split(',')[1]);
};

function calculatePageTitleNumber(pageTitles, pageTitle) {
  if (pageTitle['ui:title'] && pageTitles) {
    return (pageTitles === null || pageTitles === void 0 ? void 0 : pageTitles.findIndex(function (obj) {
      var _pageTitle$uiOptions, _pageTitle$uiOptions$;

      return obj.content === pageTitle['ui:title'] && obj.key === ((_pageTitle$uiOptions = pageTitle['ui:options']) === null || _pageTitle$uiOptions === void 0 ? void 0 : (_pageTitle$uiOptions$ = _pageTitle$uiOptions.element) === null || _pageTitle$uiOptions$ === void 0 ? void 0 : _pageTitle$uiOptions$.key);
    })) + 1 + '. ';
  }
}

var _default = function _default(_ref) {
  var _uiSchema$uiOptions2, _uiSchema$uiOptions3, _element2$content, _uiSchema$uiOptions5;

  var formData = _ref.formData,
      schema = _ref.schema,
      uiSchema = _ref.uiSchema,
      pageTitles = _ref.pageTitles,
      reviewProps = _ref.reviewProps;
  var intl = (0, _reactIntl.useIntl)();
  var locale = intl.locale;
  var classes = useStyles();

  if (schema.enumNames) {
    if (schema.inline) {
      formData = formData.map(function (data) {
        return schema.enumNames[schema["enum"].indexOf(data)];
      }).join(', ');
    } else {
      formData = schema.enumNames[schema["enum"].indexOf(formData)];
    }
  }

  if (((uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema['ui:field']) === _stepTitle.StepTitle || (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema['ui:field']) === _title.TitleField) && uiSchema['ui:title']) {
    return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_Typography["default"], {
        variant: "h5",
        component: "h3",
        style: {
          paddingTop: "35px"
        },
        children: [calculatePageTitleNumber(pageTitles, uiSchema), uiSchema['ui:title']]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("hr", {
        style: {
          border: "1px solid"
        }
      })]
    });
  }

  if (schema.additionalType === 'image') {
    var _uiSchema$uiOptions;

    var element = uiSchema === null || uiSchema === void 0 ? void 0 : (_uiSchema$uiOptions = uiSchema['ui:options']) === null || _uiSchema$uiOptions === void 0 ? void 0 : _uiSchema$uiOptions.element;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("img", {
      src: element.url,
      alt: element.altText,
      className: classes.image
    });
  }

  if (schema.format === 'date') {
    try {
      var _reviewProps$dateForm;

      formData = formData && (0, _dateFns.format)(new Date(formData), (_reviewProps$dateForm = reviewProps === null || reviewProps === void 0 ? void 0 : reviewProps.dateFormat) !== null && _reviewProps$dateForm !== void 0 ? _reviewProps$dateForm : 'd.M.yyyy');
    } catch (e) {
      // eslint-disable-next-line no-console
      console.warn("".concat(reviewProps === null || reviewProps === void 0 ? void 0 : reviewProps.dateFormat, " is not a valid dateformat, using default format d.M.yyyy"));
      formData = formData && (0, _dateFns.format)(new Date(formData), 'd.M.yyyy');
    }
  }

  if ((uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema['ui:widget']) === 'password') {
    formData = '********';
  }

  if ((uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema['ui:widget']) === 'range') {
    var _element = uiSchema['ui:options'].element;

    if ((_element === null || _element === void 0 ? void 0 : _element.widget) === 'customScale') {
      var scaleMarks = (0, _lodash.sortBy)(uiSchema['ui:options'].element.scaleMarks, 'value').filter(function (scale) {
        return scale.showInReview;
      });
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography["default"], {
          variant: "caption",
          children: scaleMarks.length > 0 ? scaleMarks.map(function (_ref2) {
            var value = _ref2.value,
                label = _ref2.label;
            return value + ' = ' + label;
          }).join(', ') : intl.formatMessage({
            id: "Q2D7I9",
            defaultMessage: [{
              "type": 0,
              "value": "Arvoväliltä "
            }, {
              "type": 1,
              "value": "minimum"
            }, {
              "type": 0,
              "value": " - "
            }, {
              "type": 1,
              "value": "maximum"
            }]
          }, {
            minimum: _element.minimum,
            maximum: _element.maximum
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Data, {
          children: formData
        })]
      });
    } else if ((_element === null || _element === void 0 ? void 0 : _element.minimum) !== undefined && (_element === null || _element === void 0 ? void 0 : _element.maximum) !== undefined) {
      var minimum = _element.minimum,
          maximum = _element.maximum;
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography["default"], {
          variant: "caption",
          children: intl.formatMessage({
            id: "Q2D7I9",
            defaultMessage: [{
              "type": 0,
              "value": "Arvoväliltä "
            }, {
              "type": 1,
              "value": "minimum"
            }, {
              "type": 0,
              "value": " - "
            }, {
              "type": 1,
              "value": "maximum"
            }]
          }, {
            minimum: minimum,
            maximum: maximum
          })
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Data, {
          children: formData
        })]
      });
    }
  }

  if (schema.format === 'data-url') {
    if (formData) {
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [decodeURIComponent(formData.match( /*#__PURE__*/(0, _wrapRegExp2["default"])(/name=(.*);/, {
          name: 1
        })).groups.name), ' ', "(", (0, _prettyBytes["default"])(dataUrlByteLength(formData), {
          locale: locale
        }), ")"]
      });
    }
  }

  if (!(uiSchema === null || uiSchema === void 0 ? void 0 : (_uiSchema$uiOptions2 = uiSchema['ui:options']) === null || _uiSchema$uiOptions2 === void 0 ? void 0 : _uiSchema$uiOptions2.dummy)) {
    var _formData;

    (_formData = formData) !== null && _formData !== void 0 ? _formData : formData = _.empty;
  }

  if (uiSchema === null || uiSchema === void 0 ? void 0 : (_uiSchema$uiOptions3 = uiSchema['ui:options']) === null || _uiSchema$uiOptions3 === void 0 ? void 0 : _uiSchema$uiOptions3.dummy) {
    var _uiSchema$uiOptions4;

    var _element2 = uiSchema === null || uiSchema === void 0 ? void 0 : (_uiSchema$uiOptions4 = uiSchema['ui:options']) === null || _uiSchema$uiOptions4 === void 0 ? void 0 : _uiSchema$uiOptions4.element;

    switch (_element2 === null || _element2 === void 0 ? void 0 : _element2.type) {
      case 'body':
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Markdown["default"], {
          children: (_element2$content = _element2.content) === null || _element2$content === void 0 ? void 0 : _element2$content.replace('\\*', '*')
        });

      case 'title':
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography["default"], {
          variant: "h6",
          component: "h3",
          children: _element2 === null || _element2 === void 0 ? void 0 : _element2.content
        });

      case 'subtitle':
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography["default"], {
          variant: "h6",
          component: "p",
          children: _element2 === null || _element2 === void 0 ? void 0 : _element2.content
        });

      default:
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography["default"], {
          variant: "body1",
          children: _element2 === null || _element2 === void 0 ? void 0 : _element2.content
        });
    }
  }

  if (schema.format === 'markdown') {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Markdown["default"], {
      children: formData.replace('\\*', '*')
    });
  }

  if (uiSchema === null || uiSchema === void 0 ? void 0 : (_uiSchema$uiOptions5 = uiSchema['ui:options']) === null || _uiSchema$uiOptions5 === void 0 ? void 0 : _uiSchema$uiOptions5.unit) {
    formData = "".concat(formData, " ").concat(uiSchema['ui:options'].unit);
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Data, {
    children: formData
  });
};

exports["default"] = _default;

function Data(_ref3) {
  var children = _ref3.children;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    style: {
      whiteSpace: 'pre-line',
      overflowWrap: 'break-word'
    },
    children: children
  });
}
//# sourceMappingURL=string.cjs.map