import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import { defineMessage } from "react-intl";
import TextField from "@material-ui/core/TextField";
import { makeStyles } from "@material-ui/core/styles";
import { utils } from "@visma/rjsf-core";
import Typography from "@material-ui/core/Typography";
import { defineMessages } from "@formatjs/intl";
import { FormHelperText } from "@material-ui/core";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
var useStyles = 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__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(FormHelperText, {
      id: "".concat(idSchema.$id, "__description"),
      component: "span",
      children: options.description
    }), /*#__PURE__*/_jsx(TextField, {
      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(_objectSpread(_objectSpread({}, formData), {}, {
          value: e.target.value
        }));
      },
      "aria-label": utils.generateAriaLabel(options.useLabel ? options.label : options.title, options, required),
      "aria-labelledby": utils.ariaDescribedBy(props.id, uiSchema, rawErrors)
    }), /*#__PURE__*/_jsxs(Typography, {
      "aria-hidden": true,
      id: "".concat(idSchema.confirmation.$id, "-title"),
      component: "p",
      style: {
        paddingTop: 30
      },
      variant: "subtitle1",
      children: [options.confirmTitle, props.required && " *"]
    }), /*#__PURE__*/_jsx(TextField, {
      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(_objectSpread(_objectSpread({}, formData), {}, {
          confirmation: e.target.value
        }));
      }
    })]
  });
};

export default (function (_ref) {
  var config = _ref.config;
  return {
    schema: {
      type: 'object',
      format: 'confirmField',
      properties: {
        value: {
          type: 'string'
        },
        confirmation: {
          type: 'string'
        }
      }
    },
    uiSchema: {
      'ui:field': ConfirmField
    }
  };
});
export var name = defineMessage({
  id: "WE/gs/",
  defaultMessage: [{
    "type": 0,
    "value": "Sähköpostiosoite vahvistuksella"
  }]
});
export var elementType = 'field';
var validationMessages = 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"
    }]
  }
});
export var validators = {
  validate: {
    name: defineMessage({
      id: "GwKRYM",
      defaultMessage: [{
        "type": 0,
        "value": "Kenttien vertaaminen"
      }]
    }),
    fn: function fn(value, element) {
      if (value) {
        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;
          }
        }
      }
    }
  }
};
//# sourceMappingURL=confirmField.js.map