import { r as registerInstance, c as createEvent, h, H as Host } from './index-55faf181.js';
import { T as TextField } from './index-403e215e.js';
import { V as ValidatorEmail } from './ValidatorEmail-df7a1b77.js';
import './kimlikApi-7350588e.js';
import './data-transfer-field-918730a3.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance } from './instance-46ab1e99.js';
import { c as convertMentornityFieldToDevingenField } from './mentorinty-bridge-api-946adc0d.js';
import { g as getSessionDetails, m as mentornityApiEndpoint } from './session-3ec0aa4c.js';
import './BaseField-461b6d0c.js';
import './InterceptorHandler-6474bcf4.js';
import './FieldModelGenerator-1ee7fda2.js';
import './index-6a1cf9c1.js';
import './date-ce777316.js';
import './index-083a6906.js';
import './index-fa1db470.js';
import './index-ebde6a66.js';
import './attachment-32decf79.js';
import './data-model-f5160369.js';
import './membership-61c4a2f7.js';
import './membership-config-a51a5232.js';
import './table-config-5c917348.js';
import './string-3065a411.js';
import './User-d2ee8b71.js';
import './CalendarConfig-184230a0.js';
import './UserSettings-7b80b841.js';
import './cookie-93ea205f.js';
import './_commonjsHelpers-27f122dd.js';
import './index-d3bd30bd.js';

const dvnSwitchCss = "dvn-switch{--margin:var(--spacing-unit) calc(1.5 * var(--spacing-unit));--switch-cursor:pointer;--switch-border-radius:var(--corner-radius);--switch-controller-border-radius:calc(var(--corner-radius) - 2px);display:inline-block;height:var(--switch-height);width:var(--switch-width);background:var(--base-color-lightest);border-radius:var(--switch-border-radius);padding:var(--switch-padding);cursor:var(--switch-cursor);-webkit-transition:background-color 0.3s, -webkit-box-shadow 0.3s;transition:background-color 0.3s, -webkit-box-shadow 0.3s;transition:background-color 0.3s, box-shadow 0.3s;transition:background-color 0.3s, box-shadow 0.3s, -webkit-box-shadow 0.3s;}dvn-switch.disabled{--switch-cursor:unset;background:var(--color-neutral-lightest)}dvn-switch.disabled .switch-controller{background-color:var(--color-disabled)}dvn-switch .switch-controller{-webkit-transition:margin-left 0.3s, background-color 0.3s;transition:margin-left 0.3s, background-color 0.3s;background-color:var(--base-color);width:var(--switch-controller-size);height:var(--switch-controller-size);border-radius:var(--switch-controller-border-radius);margin-left:var(--switch-controller-size)}dvn-switch .switch-controller.to-checked{-webkit-animation:to-checked-animation 0.3s 0s both;animation:to-checked-animation 0.3s 0s both}dvn-switch .switch-controller.to-unchecked{-webkit-animation:to-unchecked-animation 0.3s 0s both;animation:to-unchecked-animation 0.3s 0s both}dvn-switch:hover:not(.disabled) .switch-controller{background-color:var(--base-color-light)}dvn-switch:focus{outline:none}dvn-switch:focus:not(.disabled){-webkit-box-shadow:0 1px 10px var(--base-color-light);box-shadow:0 1px 10px var(--base-color-light)}dvn-switch:focus:not(.disabled) .switch-controller{background-color:var(--base-color-light)}dvn-switch.passive{--base-color:var(--color-neutral);--base-color-light:var(--color-neutral-light);--base-color-lightest:var(--color-neutral-lightest)}dvn-switch.passive:focus:not(.disabled){outline:none}dvn-switch.passive .switch-controller{margin-left:0}dvn-switch.neutral{--base-color:var(--color-neutral);--base-color-light:var(--color-neutral-light);--base-color-lightest:var(--color-neutral-lightest)}dvn-switch.primary:not(.passive){--base-color:var(--color-primary);--base-color-light:var(--color-primary-light);--base-color-lightest:var(--color-primary-lightest)}dvn-switch.secondary:not(.passive){--base-color:var(--color-secondary);--base-color-light:var(--color-secondary-light);--base-color-lightest:var(--color-secondary-lightest)}dvn-switch.warning:not(.passive){--base-color:var(--color-warning);--base-color-light:var(--color-warning-light);--base-color-lightest:var(--color-warning-lightest)}dvn-switch.danger:not(.passive){--base-color:var(--color-danger);--base-color-light:var(--color-danger-light);--base-color-lightest:var(--color-danger-lightest)}dvn-switch.rounded{--switch-border-radius:var(--switch-height);--switch-controller-border-radius:var(--switch-height)}dvn-switch.rectange{--switch-border-radius:var(--switch-height)}dvn-switch.small{--font-size:var(--font-size-small);--switch-height:24px;--switch-width:44px;--switch-controller-size:20px;--switch-padding:2px}dvn-switch.medium{--font-size:var(--font-size-medium);--switch-height:32px;--switch-width:56px;--switch-controller-size:24px;--switch-padding:4px}dvn-switch.large{--font-size:var(--font-size-large);--switch-height:40px;--switch-width:68px;--switch-controller-size:28px;--switch-padding:6px}dvn-switch.leaned{--margin:0}@-webkit-keyframes to-checked-animation{0%{margin-left:0}100%{margin-left:var(--switch-controller-size)}}@keyframes to-checked-animation{0%{margin-left:0}100%{margin-left:var(--switch-controller-size)}}@-webkit-keyframes to-unchecked-animation{0%{margin-left:var(--switch-controller-size)}100%{margin-left:0}}@keyframes to-unchecked-animation{0%{margin-left:var(--switch-controller-size)}100%{margin-left:0}}";

const DvnSwitch = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.change = createEvent(this, "change", 7);
    this.internalValue = false;
    this.tint = 'primary';
    this.scale = 'medium';
    this.leaned = false;
    this.disabled = false;
    this.shape = 'rectangular';
    this.checked = false;
    this.onClick = () => {
      if (this.disabled) {
        return;
      }
      this.internalValue = !this.internalValue;
      // this is used to show the animation when the parent component re-renders
      // and the value of this component changes
      this.animationClass = this.internalValue ? 'to-checked' : 'to-unchecked';
      setTimeout(() => this.animationClass = '', 300);
      this.change.emit(this.internalValue);
    };
  }
  componentWillLoad() {
    this.internalValue = this.checked;
  }
  componentShouldUpdate(newValue, _oldValue, name) {
    // prevent re-render if the component already has the same value internally
    return !(name === 'checked' && this.internalValue === newValue);
  }
  watchHandler(newValue) {
    if (this.internalValue !== newValue) {
      // update the internal value if the value is changed externally
      this.internalValue = newValue;
    }
  }
  render() {
    return (h(Host, { tabindex: "0", class: {
        [this.tint]: true,
        [this.scale]: true,
        [this.shape]: true,
        leaned: this.leaned,
        disabled: this.disabled,
        passive: !this.internalValue,
      }, disabled: this.disabled, onClick: this.onClick, onKeyDown: (event) => {
        if (event.keyCode === 32) {
          event.preventDefault();
          this.onClick();
          return false;
        }
      } }, h("div", { class: { 'switch-controller': true, [this.animationClass]: true } })));
  }
  static get watchers() { return {
    "checked": ["watchHandler"]
  }; }
};
DvnSwitch.style = dvnSwitchCss;

const en = {
  applicationIsSent: 'Your application is sent.',
  applicationFailed: 'Sending the application failed.',
  firstName: 'First name',
  lastName: 'Last name',
  email: 'Email',
  sendApplication: 'Send Application',
};
const tr = {
  applicationIsSent: 'Başvurunuz gönderildi.',
  applicationFailed: 'Başvuru gönderilirken hata oluştu.',
  firstName: 'İsim',
  lastName: 'Soyisim',
  email: 'E-posta',
  sendApplication: 'Başvuruyu Gönder',
};

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 [
    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',
    }
  ];
};
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 { context: { organisationId }, headers } = getSessionDetails();
    this.api = createMentornityApiInstance(mentornityApiEndpoint, headers);
    const organisation = await this.api.getOrganisation(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(organisationId)
        .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(organisationId, collection)
        .then(form => this.form = form)
        .catch(error => this.error = error);
    }
  }
  render() {
    const { error, form, formFromEditor, language, organisation, organisationConfig, isComplete, isProgressing } = this;
    const formToUse = formFromEditor || form;
    const i18n = language === 'tr' ? tr : en;
    if (!formToUse && error && error.status === 404) {
      return (h("mentornity-root-wrapper", { class: "d-flex flex-column align-items-center" }, h("p", { class: "p-3" }, "Applications are not open.")));
    }
    if (!formToUse || !organisation) {
      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" }, "Applications are not open.")));
    }
    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, coverBrightness: 50, coverSrc: organisation.coverUrl || 'https://bit.ly/36goixD', showImage: true, imageSrc: organisation.logoUrl, imageShape: "rounded", imageBorder: true }), h("dvn-markdown", { content: formToUse.description })), !isComplete &&
      h("dvn-card", { leanedSides: true }, h("doldurma-form", { fields: formFields(i18n, organisationConfig, formToUse), onSubmit: this.onSubmit, submitButtonLabel: i18n.sendApplication, progressing: isProgressing || isComplete, formErrors: [] })), isComplete &&
      h("dvn-card", { leanedSides: true, tint: "secondary" }, i18n.applicationIsSent), error &&
      h("dvn-card", { leanedSides: true, tint: "danger" }, i18n.applicationFailed))));
  }
};

export { DvnSwitch as dvn_switch, MentornityMembershipApplicationWrapper as mentornity_membership_application_wrapper };
