import { r as registerInstance, h } from './index-ecdfb761.js';
import { T as TextField } from './index-150b6a59.js';
import { V as ValidatorEmail } from './ValidatorEmail-df7a1b77.js';
import './apiBase-6015f41f.js';
import './data-transfer-field-2f305919.js';
import './filter-cbaa3d7d.js';
import { g as getSessionDetails, c as createMentornityApiInstance, m as mentornityApiEndpoint } from './session-2a491341.js';
import { b as convertMentornityFieldToDevingenField } from './mentorinty-bridge-api-5ca2d652.js';
import { I as ImagePosition } from './models-3a85e626.js';
import { s as sozlukEntries, h as MentornityMembershipApplicationSettingsI18nKey, i as mentornityMembershipApplicationSettingsI18nDefault } from './sozluk-entries-1e8f5234.js';
import { S as SozlukApi } from './index-3bf9b308.js';
import './FieldGenerator-3ae403b2.js';
import './BaseField-0b01dc2c.js';
import './InterceptorHandler-6474bcf4.js';
import './membership-b6639471.js';
import './data-model-88af49fc.js';
import './table-config-d73a72ab.js';
import './FieldModelGenerator-378c8613.js';
import './reference-field-a5b414d1.js';
import './index-61dcd269.js';
import './date-1bc489bd.js';
import './index-4e451db4.js';
import './index-23baeeef.js';
import './index-09efdc01.js';
import './string-3065a411.js';
import './User-72c4643a.js';
import './UserSettings-7e4170a7.js';
import './cookie-5d9f5819.js';
import './kimlik-api-fddeac81.js';
import './_commonjsHelpers-27f122dd.js';
import './index-d3bd30bd.js';
import './calendar-470c802a.js';
import './index-a3ca021c.js';
import './moment-65eca6b3.js';
import './index-e66e8eca.js';
import './attachment-961dd228.js';
import './CalendarConfig-412d1b0f.js';
import './i18n-09a07b97.js';
import './i18n-0e757ca5.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 convertMentornityFieldToDevingenField(fieldFromConfig);
      }
      return undefined;
    }).filter(f => !!f);
  }
  return [
    new TextField('firstName', i18n.firstName, '', '').setErrorTitle(i18n.firstName)
      .require(),
    new TextField('lastName', i18n.lastName, '', '').setErrorTitle(i18n.lastName)
      .require(),
    new TextField('email', i18n.email, '', '', 'email').setErrorTitle(i18n.email)
      .addInterceptor('onSubmit', new ValidatorEmail())
      .require(),
    ...extraFields,
    {
      id: 'mentornityApplicationLegalFooter',
      customViewTag: 'mentornity-application-legal-footer',
      i18n
    }
  ];
};
const MentornityMembershipApplicationWrapper = class {
  constructor(hostRef) {
    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 } = 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 } = getSessionDetails();
    this.api = createMentornityApiInstance(mentornityApiEndpoint, headers);
    // Setup configuration
    const apiSozluk = new SozlukApi().setLanguage(this.language).setContents(sozlukEntries);
    this.strings = await apiSozluk.getContent(MentornityMembershipApplicationSettingsI18nKey, 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 (h("mentornity-root-wrapper", { class: "d-flex flex-column align-items-center" }, h("p", { class: "p-3" }, strings.appsAreNotOpen)));
    }
    if (!formToUse || !organisation || !strings) {
      return (h("mentornity-root-wrapper", null, h("div", { class: "d-flex justify-content-center pt-3" }, h("dvn-progress", { indefinite: true }))));
    }
    if (formToUse.status === 'unpublished') {
      return (h("mentornity-root-wrapper", { class: "d-flex flex-column align-items-center" }, h("p", { class: "p-3" }, strings.appsAreNotOpen)));
    }
    return (h("mentornity-root-wrapper", { class: "d-block p-3" }, h("div", { style: { margin: '0 auto', maxWidth: `600px` } }, h("dvn-card", { leanedSides: true }, 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: ImagePosition.BelowCover }, h("div", { slot: "subtitle" }, this.organisation.title)), h("dvn-markdown", { content: formToUse.description })), !isComplete &&
      h("dvn-card", { leanedSides: true }, h("doldurma-form", { fields: formFields(strings, organisationConfig, formToUse), onSubmit: this.onSubmit, submitButtonLabel: strings.sendApplication, progressing: isProgressing || isComplete, formErrors: [] })), isComplete &&
      h("dvn-card", { leanedSides: true, tint: "secondary" }, strings.applicationIsSent), error &&
      h("dvn-card", { leanedSides: true, tint: "danger" }, strings.applicationFailed))));
  }
};

export { MentornityMembershipApplicationWrapper as mentornity_membership_application_wrapper };
