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 { STORAGE_KEY_KIMLIK_TOKEN, STORAGE_KEY_KIMLIK_USER_ID } from '@dvn/kimlik/api/kimlik-api';
import { CookieStorage } from '@dvn/kimlik/storage/cookie';
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())
    .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 LoginForm {
  constructor() {
    /**
     * Data to pre-fill.
     */
    this.data = {};
    this.storage = new CookieStorage();
    this.onSubmit = (event) => {
      const data = event.detail.values;
      this.progressing = true;
      this.api.login(data.email, data.password).then(this.onLoginSucceed, this.onError);
    };
    this.onLoginSucceed = (loginData) => {
      this.api.addAuthInterceptor(loginData.jwt);
      this.progressing = false;
      this.storage.save(STORAGE_KEY_KIMLIK_TOKEN, loginData.jwt);
      this.storage.save(STORAGE_KEY_KIMLIK_USER_ID, loginData.userId);
      if (this.onLogin) {
        this.onLogin(loginData);
      }
      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: loginFormFields(data), onSubmit: this.onSubmit, submitButtonLabel: "Login", progressing: progressing, formErrors: getFormErrors(error) }));
  }
  static get is() { return "kimlik-login-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": "{}"
    },
    "onLogin": {
      "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/kimlik-api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "storage": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "Storage",
        "resolved": "Storage",
        "references": {
          "Storage": {
            "location": "import",
            "path": "@dvn/kimlik/storage/Storage.interface"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "defaultValue": "new CookieStorage()"
    }
  }; }
  static get states() { return {
    "progressing": {},
    "error": {}
  }; }
}
