import _wrapRegExp from "@babel/runtime/helpers/wrapRegExp";
import Typography from '@material-ui/core/Typography';
import { byteLength } from 'base64-js';
import { format } from 'date-fns';
import { sortBy } from 'lodash';
import prettyBytes from 'pretty-bytes';
import { useIntl } from 'react-intl';
import { empty } from '..';
import { StepTitle } from '../../configToSchemas/types/stepTitle';
import { TitleField } from '../../configToSchemas/types/title';
import Markdown from '../../Markdown';
import { jsxs as _jsxs } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";

var dataUrlByteLength = function dataUrlByteLength(dataUrl) {
  return 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 + '. ';
  }
}

export default (function (_ref) {
  var _uiSchema$uiOptions, _uiSchema$uiOptions2, _element$content, _uiSchema$uiOptions4;

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

  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 || (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema['ui:field']) === TitleField) && uiSchema['ui:title']) {
    return /*#__PURE__*/_jsxs(_Fragment, {
      children: [/*#__PURE__*/_jsxs(Typography, {
        variant: "h5",
        component: "h3",
        style: {
          paddingTop: "35px"
        },
        children: [calculatePageTitleNumber(pageTitles, uiSchema), uiSchema['ui:title']]
      }), /*#__PURE__*/_jsx("hr", {
        style: {
          border: "1px solid"
        }
      })]
    });
  }

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

      formData = formData && 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 && 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 = sortBy(uiSchema['ui:options'].element.scaleMarks, 'value').filter(function (scale) {
        return scale.showInReview;
      });
      return /*#__PURE__*/_jsxs(_Fragment, {
        children: [/*#__PURE__*/_jsx(Typography, {
          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__*/_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__*/_jsxs(_Fragment, {
        children: [/*#__PURE__*/_jsx(Typography, {
          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__*/_jsx(Data, {
          children: formData
        })]
      });
    }
  }

  if (schema.format === 'data-url') {
    if (formData) {
      return /*#__PURE__*/_jsxs(_Fragment, {
        children: [decodeURIComponent(formData.match( /*#__PURE__*/_wrapRegExp(/name=(.*);/, {
          name: 1
        })).groups.name), ' ', "(", prettyBytes(dataUrlByteLength(formData), {
          locale: locale
        }), ")"]
      });
    }
  }

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

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

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

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

    switch (_element === null || _element === void 0 ? void 0 : _element.type) {
      case 'body':
        return /*#__PURE__*/_jsx(Markdown, {
          children: (_element$content = _element.content) === null || _element$content === void 0 ? void 0 : _element$content.replace('\\*', '*')
        });

      case 'title':
        return /*#__PURE__*/_jsx(Typography, {
          variant: "h6",
          component: "h3",
          children: _element === null || _element === void 0 ? void 0 : _element.content
        });

      case 'subtitle':
        return /*#__PURE__*/_jsx(Typography, {
          variant: "h6",
          component: "p",
          children: _element === null || _element === void 0 ? void 0 : _element.content
        });

      default:
        return /*#__PURE__*/_jsx(Typography, {
          variant: "body1",
          children: _element === null || _element === void 0 ? void 0 : _element.content
        });
    }
  }

  if (schema.format === 'markdown') {
    return /*#__PURE__*/_jsx(Markdown, {
      children: formData.replace('\\*', '*')
    });
  }

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

  return /*#__PURE__*/_jsx(Data, {
    children: formData
  });
});

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