'use strict';

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

const index = require('./index-1141a245.js');
const index$2 = require('./index-61f9621a.js');
const ValidatorEmail = require('./ValidatorEmail-4449f08a.js');
require('./apiBase-6c66ff1b.js');
require('./data-transfer-field-e8417d5f.js');
require('./filter-d5040f65.js');
const session = require('./session-f588baa3.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-b1eaa56d.js');
const models = require('./models-8b357ee7.js');
const sozlukEntries = require('./sozluk-entries-3b9686e7.js');
const index$1 = require('./index-a2e99c85.js');
require('./FieldGenerator-279a646b.js');
require('./BaseField-381327a9.js');
require('./InterceptorHandler-09a1e84b.js');
require('./membership-e87e02ed.js');
require('./data-model-01c368bc.js');
require('./table-config-969d3038.js');
require('./FieldModelGenerator-d61f01d4.js');
require('./reference-field-1239b0c3.js');
require('./index-2eea69a0.js');
require('./date-a24d7714.js');
require('./index-0783d14f.js');
require('./index-9d3d3d15.js');
require('./index-9fd84b93.js');
require('./string-de672b69.js');
require('./User-dbfbd45e.js');
require('./UserSettings-2a2399b1.js');
require('./cookie-e64bc800.js');
require('./kimlik-api-663f4884.js');
require('./_commonjsHelpers-206db00d.js');
require('./index-33e96540.js');
require('./calendar-e4b7baeb.js');
require('./index-b776fc14.js');
require('./moment-7a3433f2.js');
require('./index-b7855ed6.js');
require('./attachment-a0cdf203.js');
require('./CalendarConfig-998f742e.js');
require('./i18n-0bcf144d.js');
require('./i18n-77d45414.js');

const formFields = (i18n, config, form) => {
  let extraFields = [];
  if (form) {
    const fieldsFromConfig = config.metadatas[form.target];
    extraFields = form.form.fields.map(fieldInForm => {
      // get the field from config because it contains the most up to date info and the form field only contains the ids
      const fieldFromConfig = fieldsFromConfig.find(f => f.id === fieldInForm.id);
      if (fieldFromConfig) {
        // field may have been deleted from the config. ignore it if deleted
        return mentorintyBridgeApi.convertMentornityFieldToDevingenField(fieldFromConfig);
      }
      return undefined;
    }).filter(f => !!f);
  }
  return [
    new index$2.TextField('firstName', i18n.firstName, '', '').setErrorTitle(i18n.firstName)
      .require(),
    new index$2.TextField('lastName', i18n.lastName, '', '').setErrorTitle(i18n.lastName)
      .require(),
    new index$2.TextField('email', i18n.email, '', '', 'email').setErrorTitle(i18n.email)
      .addInterceptor('onSubmit', new ValidatorEmail.ValidatorEmail())
      .require(),
    ...extraFields,
    {
      id: 'mentornityApplicationLegalFooter',
      customViewTag: 'mentornity-application-legal-footer',
      i18n
    }
  ];
};
const MentornityMembershipApplicationWrapper = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.isProgressing = false;
    this.isComplete = false;
    this.onSubmit = (event) => {
      if (this.onAction) {
        // the page is used in builder
        this.onAction('submit');
        return;
      }
      const { context } = session.getSessionDetails();
      const data = event.detail.values;
      this.isProgressing = true;
      const collection = this.formTarget === 'mentor' ? 'mentors' : 'mentees';
      this.api.createMembershipApplication(context.organisationId, collection, data).then(this.onSucceed, this.onError);
    };
    this.onSucceed = () => {
      this.isProgressing = false;
      this.isComplete = true;
    };
    this.onError = (error) => {
      this.error = error;
      this.isProgressing = false;
    };
  }
  async componentWillLoad() {
    const { headers } = session.getSessionDetails();
    this.api = session.createMentornityApiInstance(session.mentornityApiEndpoint, headers);
    // Setup configuration
    const apiSozluk = new index$1.SozlukApi().setLanguage(this.language).setContents(sozlukEntries.sozlukEntries);
    this.strings = await apiSozluk.getContent(sozlukEntries.MentornityMembershipApplicationSettingsI18nKey, sozlukEntries.mentornityMembershipApplicationSettingsI18nDefault);
    const organisation = await this.api.getOrganisation(this.organisationId)
      .catch(error => this.error = error);
    if (!organisation)
      return;
    this.organisation = organisation;
    if (this.configFromEditor) {
      this.organisationConfig = this.configFromEditor;
    }
    else {
      const config = await this.api.getOrganisationConfig(organisation._id)
        .catch(error => this.error = error);
      if (!config)
        return;
      this.organisationConfig = config;
    }
    if (this.formFromEditor) {
      this.form = this.formFromEditor;
    }
    else {
      const collection = this.formTarget === 'mentor' ? 'mentors' : 'mentees';
      this.api.getMembershipApplicationForm(organisation._id, collection)
        .then(form => this.form = form)
        .catch(error => this.error = error);
    }
  }
  render() {
    const { error, form, formFromEditor, organisation, organisationConfig, isComplete, isProgressing, strings } = this;
    const formToUse = formFromEditor || form;
    if (!formToUse && error && error.status === 404) {
      return (index.h("mentornity-root-wrapper", { class: "d-flex flex-column align-items-center" }, index.h("p", { class: "p-3" }, strings.appsAreNotOpen)));
    }
    if (!formToUse || !organisation || !strings) {
      return (index.h("mentornity-root-wrapper", null, index.h("div", { class: "d-flex justify-content-center pt-3" }, index.h("dvn-progress", { indefinite: true }))));
    }
    if (formToUse.status === 'unpublished') {
      return (index.h("mentornity-root-wrapper", { class: "d-flex flex-column align-items-center" }, index.h("p", { class: "p-3" }, strings.appsAreNotOpen)));
    }
    return (index.h("mentornity-root-wrapper", { class: "d-block p-3" }, index.h("div", { style: { margin: '0 auto', maxWidth: `600px` } }, index.h("dvn-card", { leanedSides: true }, index.h("dvn-card-header", { title: formToUse.title, 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" }, this.organisation.title)), index.h("dvn-markdown", { content: formToUse.description })), !isComplete &&
      index.h("dvn-card", { leanedSides: true }, index.h("doldurma-form", { fields: formFields(strings, organisationConfig, formToUse), onSubmit: this.onSubmit, submitButtonLabel: strings.sendApplication, progressing: isProgressing || isComplete, formErrors: [] })), isComplete &&
      index.h("dvn-card", { leanedSides: true, tint: "secondary" }, strings.applicationIsSent), error &&
      index.h("dvn-card", { leanedSides: true, tint: "danger" }, strings.applicationFailed))));
  }
};

exports.mentornity_membership_application_wrapper = MentornityMembershipApplicationWrapper;
