import { BooleanField } from '@dvn/core/doldurma/src/fields/boolean';
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 loginFormFields = (data) => ([
  new TextField('email', '', 'Email', '', 'email').setValue(data.email)
    .setErrorTitle('Email')
    .addInterceptor('onSubmit', new ValidatorEmail())
    .disable()
    .require(),
  new TextField('firstName', '', 'First Name', '', 'text').setValue(data.firstName)
    .setErrorTitle('First name')
    .addInterceptor('onSubmit', new ValidatorLength(2, 24))
    .require(),
  new TextField('lastName', '', 'Last Name', '', 'text').setValue(data.lastName)
    .setErrorTitle('Last name')
    .addInterceptor('onSubmit', new ValidatorLength(2, 24))
    .require(),
  new TextField('password', '', 'Password', '', 'password').setAutoComplete('off')
    .setErrorTitle('Password')
    .addInterceptor('onSubmit', new ValidatorLength(6, 24))
    .require(),
  new BooleanField('constent', '', '', 'I read and agree on terms')
    .setErrorTitle('Consent')
    .require(),
]);
function getFormErrors(error) {
  if (!error) {
    return undefined;
  }
  if (error.status === 401) {
    return ['Credentials are not valid.'];
  }
  else {
    return ['Unexpected error.'];
  }
}
export class KimlikActivateUserForm {
  constructor() {
    this.data = {};
    this.onSubmit = (event) => {
      const { email, firstName, lastName, password } = event.detail.values;
      this.progressing = true;
      this.api.activateUser(email, firstName, lastName, password).then(this.onActivateSucceed, this.onError);
    };
    this.onActivateSucceed = (loginData) => {
      this.api.addAuthInterceptor(loginData.jwt);
      this.progressing = false;
      if (this.onActivate) {
        this.onActivate();
      }
    };
    this.onError = (error) => {
      this.error = error;
      this.progressing = false;
    };
  }
  render() {
    const { data, error, progressing } = this;
    return (h("doldurma-form", { fields: loginFormFields(data), onSubmit: this.onSubmit, submitButtonLabel: "Create Account", progressing: progressing, formErrors: getFormErrors(error) }));
  }
  static get is() { return "kimlik-activate-user-form"; }
  static get properties() { return {
    "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": ""
      }
    },
    "data": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "object",
        "resolved": "object",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "{}"
    },
    "onActivate": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "() => void",
        "resolved": "() => void",
        "references": {}
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "progressing": {},
    "error": {}
  }; }
}
