import { r as registerInstance, h } from './index-ecdfb761.js';
import { T as TextField } from './index-150b6a59.js';
import { V as ValidatorEmail } from './ValidatorEmail-df7a1b77.js';
import { V as ValidatorLength } from './ValidatorLength-f56a7c14.js';
import './FieldGenerator-3ae403b2.js';
import './BaseField-0b01dc2c.js';
import './InterceptorHandler-6474bcf4.js';

const formFields = (data) => ([
  new TextField('firstName', '', 'First name', '').setValue(data.firstName)
    .setErrorTitle('Name')
    .require(),
  new TextField('lastName', '', 'Last name', '').setValue(data.lastName)
    .setErrorTitle('Surname')
    .require(),
  new TextField('email', '', 'Email', '', 'email').setValue(data.email)
    .setErrorTitle('Email')
    .addInterceptor('onSubmit', new ValidatorEmail())
    .require(),
  new TextField('password', '', 'Password', '', 'password').setErrorTitle('Password')
    .addInterceptor('onSubmit', new 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) {
    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 (h("doldurma-form", { fields: formFields(data), onSubmit: this.onSubmit, submitButtonLabel: "Register", progressing: progressing, formErrors: getFormErrors(error) }));
  }
};

export { KimlikRegisterForm as kimlik_register_form };
