'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-7cdf1df1.js');
const index$1 = require('./index-c2a077ae.js');
require('./InterceptorHandler-09a1e84b.js');
require('./BaseField-f50bb8f0.js');
const ValidatorEmail = require('./ValidatorEmail-4449f08a.js');
const ValidatorLength = require('./ValidatorLength-06e5c6f8.js');

const formFields = (data) => ([
  new index$1.TextField('firstName', '', 'First name', '').setValue(data.firstName)
    .setErrorTitle('Name')
    .require(),
  new index$1.TextField('lastName', '', 'Last name', '').setValue(data.lastName)
    .setErrorTitle('Surname')
    .require(),
  new index$1.TextField('email', '', 'Email', '', 'email').setValue(data.email)
    .setErrorTitle('Email')
    .addInterceptor('onSubmit', new ValidatorEmail.ValidatorEmail())
    .require(),
  new index$1.TextField('password', '', 'Password', '', 'password').setErrorTitle('Password')
    .addInterceptor('onSubmit', new ValidatorLength.ValidatorLength(6, 24))
    .require(),
]);
function getFormErrors(error) {
  if (!error) {
    return undefined;
  }
  if (error.status === 401) {
    return ['Credentials are not valid.'];
  }
  else {
    return ['Unexpected error.'];
  }
}
const KimlikRegisterForm = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    /**
     * Data to pre-fill.
     */
    this.data = {};
    this.onSubmit = (event) => {
      const data = event.detail.values;
      const { email, firstName, lastName, password } = data;
      this.progressing = true;
      this.api.register(email, firstName, lastName, password).then(this.onSucceed, this.onError);
    };
    this.onSucceed = (data) => {
      this.api.addAuthInterceptor(data.jwt);
      this.progressing = false;
      if (this.onRegister) {
        this.onRegister(data);
      }
      if (this.redirect) {
        window.location = this.redirect;
      }
    };
    this.onError = (error) => {
      this.error = error;
      this.progressing = false;
    };
  }
  render() {
    const { data, error, progressing } = this;
    return (index.h("doldurma-form", { fields: formFields(data), onSubmit: this.onSubmit, submitButtonLabel: "Register", progressing: progressing, formErrors: getFormErrors(error) }));
  }
};

exports.kimlik_register_form = KimlikRegisterForm;
