"use strict";

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

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

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

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

var _TextField = _interopRequireDefault(require("@material-ui/core/TextField"));

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

var _rjsfCore = require("@visma/rjsf-core");

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

var _intl = require("@formatjs/intl");

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

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

var useStyles = (0, _styles.makeStyles)({
  inputLabelRoot: {
    clip: "rect(0 0 0 0)",
    clipPath: "inset(50%)",
    height: 1,
    overflow: "hidden",
    position: "absolute",
    whiteSpace: "nowrap",
    width: 1
  },
  inputFormControl: {
    "label + &": {
      marginTop: 0
    }
  }
});

var ConfirmField = function ConfirmField(props) {
  var schema = props.schema,
      uiSchema = props.uiSchema,
      formData = props.formData,
      _onChange = props.onChange,
      idSchema = props.idSchema,
      required = props.required,
      readonly = props.readonly,
      disabled = props.disabled,
      rawErrors = props.rawErrors;
  var options = uiSchema['ui:options'].element;
  var classes = useStyles();
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_core.FormHelperText, {
      id: "".concat(idSchema.$id, "__description"),
      component: "span",
      children: options.description
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_TextField["default"], {
      id: idSchema.value.$id,
      label: options.useLabel ? options.label : options.title,
      InputLabelProps: {
        shrink: false,
        className: classes.inputLabelRoot
      },
      required: required,
      readOnly: readonly,
      disabled: disabled,
      value: formData === null || formData === void 0 ? void 0 : formData.value,
      pattern: schema.pattern,
      error: rawErrors && rawErrors.length > 0,
      onChange: function onChange(e) {
        _onChange((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, formData), {}, {
          value: e.target.value
        }));
      },
      "aria-label": _rjsfCore.utils.generateAriaLabel(options.useLabel ? options.label : options.title, options, required),
      "aria-labelledby": _rjsfCore.utils.ariaDescribedBy(props.id, uiSchema, rawErrors)
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Typography["default"], {
      "aria-hidden": true,
      id: "".concat(idSchema.confirmation.$id, "-title"),
      component: "p",
      style: {
        paddingTop: 30
      },
      variant: "subtitle1",
      children: [options.confirmTitle, props.required && " *"]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_TextField["default"], {
      id: idSchema.confirmation.$id,
      label: options.confirmTitle,
      InputLabelProps: {
        shrink: false,
        className: classes.inputLabelRoot
      },
      required: required,
      readOnly: readonly,
      disabled: disabled,
      value: formData === null || formData === void 0 ? void 0 : formData.confirmation,
      pattern: schema.pattern,
      error: props.rawErrors && props.rawErrors.length > 0,
      onChange: function onChange(e) {
        _onChange((0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, formData), {}, {
          confirmation: e.target.value
        }));
      }
    })]
  });
};

var _default = function _default(_ref) {
  var config = _ref.config;
  return {
    schema: {
      type: 'object',
      format: 'confirmField',
      properties: {
        value: {
          type: 'string'
        },
        confirmation: {
          type: 'string'
        }
      }
    },
    uiSchema: {
      'ui:field': ConfirmField
    }
  };
};

exports["default"] = _default;
var name = (0, _reactIntl.defineMessage)({
  id: "WE/gs/",
  defaultMessage: [{
    "type": 0,
    "value": "Sähköpostiosoite vahvistuksella"
  }]
});
exports.name = name;
var elementType = 'field';
exports.elementType = elementType;
var validationMessages = (0, _intl.defineMessages)({
  noMatch: {
    id: "pIsJyL",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "title"
    }, {
      "type": 0,
      "value": "\" arvon täytyy olla sama kuin \""
    }, {
      "type": 1,
      "value": "confirmTitle"
    }, {
      "type": 0,
      "value": "\" arvo"
    }]
  },
  notEmail: {
    id: "YodWUT",
    defaultMessage: [{
      "type": 0,
      "value": "\""
    }, {
      "type": 1,
      "value": "title"
    }, {
      "type": 0,
      "value": "\" arvon on oltava kelvollinen sähköpostiosoite"
    }]
  }
});
var validators = {
  validate: {
    name: (0, _reactIntl.defineMessage)({
      id: "GwKRYM",
      defaultMessage: [{
        "type": 0,
        "value": "Kenttien vertaaminen"
      }]
    }),
    fn: function fn(value, element) {
      var pattern = new RegExp('[^\\s]+^$|^(.*@[a-zA-Z0-9]+[a-zA-Z0-9\\.-]*\\.[a-zA-Z0-9]{2,})$');

      if (element.required || value.value !== undefined && value.value !== "" || value.confirmation !== undefined && value.confirmation !== "") {
        if (!pattern.test(value.value)) {
          return validationMessages.notEmail;
        }

        if (value.value !== value.confirmation) {
          return validationMessages.noMatch;
        }
      }
    }
  }
};
exports.validators = validators;
//# sourceMappingURL=confirmField.cjs.map