'use strict';

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

const index = require('./index-1141a245.js');
const models = require('./models-8b357ee7.js');
const index$2 = require('./index-e99b474f.js');
const index$1 = require('./index-61f9621a.js');
const ValidatorLength = require('./ValidatorLength-06e5c6f8.js');
const cookie = require('./cookie-135bf612.js');
const index$3 = require('./index-a2e99c85.js');
const i18n = require('./i18n-5585a319.js');
require('./FieldGenerator-279a646b.js');
require('./BaseField-381327a9.js');
require('./InterceptorHandler-09a1e84b.js');
require('./kimlik-api-d3c479a3.js');
require('./apiBase-e4ebaf8b.js');
require('./data-transfer-field-e8417d5f.js');
require('./filter-d5040f65.js');
require('./_commonjsHelpers-206db00d.js');

const MentornityAcceptInvitationWidget = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.isProgressing = false;
    this.formFields = () => {
      const defaultFields = [
        new index$1.TextField('email', this.strings.email, this.invitation.issuedFor, '').setErrorTitle(this.strings.email)
          .disable(),
        new index$2.BooleanField('constent', '', '', index$3.SozlukApi.build(this.strings.agreeTermsAndPrivacy, {
          organisationTermsLink: `https://mentornity.com/terms/${this.organisation._id}/organisation`,
          organisationTitle: this.organisation.title,
          policyLink: 'https://mentornity.com/privacy-policy'
        }))
          .setLabelType('markdown')
          .require()
      ];
      if (!this.isLoggedIn) {
        // push password field when the user has not accepted invitation
        defaultFields.splice(1, 0, new index$1.TextField('password', this.strings.password, this.strings.definePassword, '').setAutoComplete('off')
          .setErrorTitle(this.strings.password)
          .addInterceptor('onSubmit', new ValidatorLength.ValidatorLength(6, 24))
          .require());
      }
      return defaultFields;
    };
    this.login = async (email, password) => {
      try {
        const response = await this.api.login(email, password, 'web-client');
        this.storage = new cookie.CookieStorage();
        this.storage.save('mt_uid', response.user._id);
        this.storage.save('mt_st', response._id);
        this.storage.save('mt_at', response.accessToken);
      }
      catch (errors) {
        this.formErrors = errors;
      }
    };
    this.saveConsentForPolicy = async () => {
      const redirect = `/${this.organisation.domain}/welcome`;
      const accessToken = this.storage.get('mt_at');
      const sessionToken = this.storage.get('mt_st');
      // set token header
      this.api.addAuthInterceptor(accessToken, sessionToken);
      try {
        const privacyPolicy = await this.api.getPrivacyPolicy(this.language || 'en');
        // save consent if user is logged in
        await this.api.saveConsentForPolicy(this.invitation.invitee._id, privacyPolicy._id, 'invitation');
        window.location = redirect;
      }
      catch (errors) {
        this.formErrors = errors;
      }
    };
    this.onSubmit = async (event) => {
      const { values: { constent, password } } = event.detail;
      this.formErrors = undefined;
      this.isProgressing = true;
      if (constent && (password || this.isLoggedIn)) {
        try {
          await this.api.acceptInvitation(this.invitation._id, password);
          if (!this.isLoggedIn) {
            await this.login(this.invitation.issuedFor, password);
            this.saveConsentForPolicy();
          }
          else {
            window.location = `/${this.organisation.domain}/welcome`;
          }
        }
        catch (errors) {
          this.formErrors = errors;
        }
      }
      this.isProgressing = false;
    };
  }
  async componentWillLoad() {
    this.strings = await this.apiSozluk.getContent(i18n.MentornityAcceptInvitationWrapperI18nKey, i18n.mentornityAcceptInvitationWrapperI18nDefault);
  }
  render() {
    const { organisation, formErrors, strings, isProgressing } = this;
    return (index.h("div", { style: { margin: '0 auto', maxWidth: `600px` } }, index.h("dvn-card", { leanedSides: true }, index.h("dvn-card-header", { title: strings.invitation, slot: "header", showCover: true, coverSrc: organisation.coverUrl || 'https://bit.ly/36goixD', showImage: true, imageSrc: organisation.logoUrl, imageShape: "rounded", imageBorder: true, imagePosition: models.ImagePosition.BelowCover }, index.h("div", { slot: "subtitle" }, organisation.title)), index.h("dvn-markdown", { content: organisation.description })), index.h("dvn-card", { leanedSides: true }, index.h("doldurma-form", { fields: this.formFields(), onSubmit: this.onSubmit, submitButtonLabel: strings.submit, progressing: isProgressing, formErrors: formErrors }))));
  }
};

exports.mentornity_accept_invitation_widget = MentornityAcceptInvitationWidget;
