import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ValidatorEmail } from '@dvn/core/doldurma/src/interceptors/validators/ValidatorEmail';
import { ValidatorLength } from '@dvn/core/doldurma/src/interceptors/validators/ValidatorLength';
import { Component, h, Prop, State } from '@stencil/core';
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.'];
  }
}
export class KimlikRegisterForm {
  constructor() {
    /**
     * 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) }));
  }
  static get is() { return "kimlik-register-form"; }
  static get properties() { return {
    "data": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "object",
        "resolved": "object",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": "Data to pre-fill."
      },
      "defaultValue": "{}"
    },
    "onRegister": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "(data: CreateSessionResponse) => void",
        "resolved": "(data: CreateSessionResponse) => void",
        "references": {
          "CreateSessionResponse": {
            "location": "import",
            "path": "@dvn/kimlik/api/dto"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "redirect": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "redirect",
      "reflect": false
    },
    "api": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "KimlikApi",
        "resolved": "KimlikApi",
        "references": {
          "KimlikApi": {
            "location": "import",
            "path": "@dvn/kimlik/api/kimlikApi"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "progressing": {},
    "error": {}
  }; }
}
