'use strict';

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

const index = require('./index-1141a245.js');
const calendar = require('./calendar-e4b7baeb.js');
const i18n = require('./i18n-4b089eee.js');
const index$1 = require('./index-a2e99c85.js');
const cookie = require('./cookie-e64bc800.js');
require('./index-b776fc14.js');
require('./_commonjsHelpers-206db00d.js');
require('./moment-7a3433f2.js');
require('./i18n-a8cc9ef1.js');
require('./kimlik-api-663f4884.js');
require('./apiBase-6c66ff1b.js');
require('./data-transfer-field-e8417d5f.js');
require('./BaseField-381327a9.js');
require('./filter-d5040f65.js');

const takvimCreateCustomAvailabilityFormCss = "takvim-create-custom-availability-form{display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;width:100%}takvim-create-custom-availability-form .time-picker-container dvn-form-field{-ms-flex:1;flex:1}takvim-create-custom-availability-form dvn-select{width:100%}takvim-create-custom-availability-form dvn-input{--width:100%}takvim-create-custom-availability-form .formError{color:var(--color-danger)}";

const defaultStartHour = 9;
const defaultEndHour = 10;
const TakvimCreateCustomAvailabilityForm = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.dismiss = index.createEvent(this, "dismiss", 7);
    this.createAvailability = index.createEvent(this, "createAvailability", 7);
    this.updateAvailability = index.createEvent(this, "updateAvailability", 7);
    this.storage = new cookie.CookieStorage();
    this.breakDuration = 0;
    this.endHour = defaultEndHour;
    this.endMinute = 0;
    this.location = '';
    this.meetingDuration = 60;
    this.startHour = defaultStartHour;
    this.startMinute = 0;
    // giving locationType lastest meeting type
    this.locationType = this.storage.get('mt_lt') || 'custom';
    this.recurrence = 'weekly';
    this.errors = [];
    this.isProgressing = false;
    this.strings = i18n.takvimCreateCustomAvailabilityFormI18nI18nDefault;
    this.isRecurring = false;
    this.recurrenceDayIndex = 0;
    this.getDayDistanceForRecurringAvailability = () => {
      return (this.recurrenceDayIndex + 7 - this.date.getDay()) % 7;
    };
    this.onStartHourChange = (e) => {
      const { hour, minute } = e.detail;
      this.startDate = calendar.CalendarUtils.createDateWithHours(this.startDate, hour, minute);
      this.startHour = hour;
      this.startMinute = minute;
    };
    this.onEndHourChange = (e) => {
      const { hour, minute } = e.detail;
      this.endDate = calendar.CalendarUtils.createDateWithHours(this.endDate, hour, minute);
      this.endHour = hour;
      this.endMinute = minute;
    };
    this.onSubmit = () => {
      const { breakDuration, location, locationType, startDate, endDate, meetingDuration, recurrence } = this;
      const hasEmptyFields = this.validateForm();
      if (hasEmptyFields)
        return;
      const availability = {
        breakDuration,
        location: location,
        recurrence: this.availability
          ? this.availability.recurrence
          : (this.isRecurring ? recurrence : 'once'),
        locationType,
        startDate: startDate.toISOString(),
        endDate: endDate.toISOString(),
        meetingDuration,
        status: 'active',
        user: {
          _ref: 'kimlik-users',
          _id: this.user._id,
        }
      };
      this.isProgressing = true;
      if (this.availability) {
        availability._id = this.availability._id;
        this.apiTakvim.updateAvailability(availability)
          .then(() => this.updateAvailability.emit())
          .finally(() => this.isProgressing = false);
      }
      else {
        this.apiTakvim.createAvailability(availability)
          .then(() => this.createAvailability.emit())
          .finally(() => this.isProgressing = false);
      }
      // store lastest location type = lt
      this.storage.save('mt_lt', locationType);
    };
    this.onClickDelete = () => {
      this.isProgressing = true;
      this.apiTakvim.deleteAvailability(this.availability._id)
        .then(() => this.updateAvailability.emit())
        .finally(() => this.isProgressing = false);
    };
    this.validateForm = () => {
      const { location, locationType, strings, meetingDuration } = this;
      let hasError = false;
      const messages = [];
      if (locationType === 'custom' && location === '') {
        messages.push(index$1.SozlukApi.build(strings.emptyFieldError, { fieldName: strings.location }));
      }
      if (!meetingDuration) {
        messages.push(index$1.SozlukApi.build(strings.emptyFieldError, { fieldName: strings.meetingDuration }));
      }
      if (!locationType) {
        messages.push(index$1.SozlukApi.build(strings.emptyFieldError, { fieldName: strings.location }));
      }
      this.errors = messages;
      hasError = messages.length > 0;
      return hasError;
    };
  }
  componentWillLoad() {
    if (this.availability) {
      // editing availability
      this.startDate = new Date(this.availability.startDate);
      this.startHour = Number(calendar.CalendarUtils.format(this.startDate, 'H'));
      this.startMinute = Number(calendar.CalendarUtils.format(this.startDate, 'm'));
      this.endDate = new Date(this.availability.endDate);
      this.endHour = Number(calendar.CalendarUtils.format(this.endDate, 'H'));
      this.endMinute = Number(calendar.CalendarUtils.format(this.endDate, 'm'));
      this.meetingDuration = this.availability.meetingDuration;
      this.breakDuration = this.availability.breakDuration;
      this.location = this.availability.location;
      this.locationType = this.availability.locationType;
      this.recurrence = this.availability.recurrence;
      return;
    }
    const startDate = new Date(this.date);
    if (this.isRecurring) { // shift date to the next possible day (e.g. if monday is past, the next monday is picked)
      startDate.setDate(startDate.getDate() + this.getDayDistanceForRecurringAvailability());
    }
    this.startDate = calendar.CalendarUtils.createDateWithHours(startDate, defaultStartHour, 0);
    const endDate = new Date(this.date);
    if (this.isRecurring) { // shift date to the next possible day (e.g. if monday is past, the next monday is picked)
      endDate.setDate(endDate.getDate() + this.getDayDistanceForRecurringAvailability());
    }
    this.endDate = calendar.CalendarUtils.createDateWithHours(endDate, defaultEndHour, 0);
  }
  render() {
    const { availability, breakDuration, endHour, endMinute, location, recurrence, meetingDuration, startHour, startMinute, locationType, isProgressing, strings, } = this;
    let date = this.date;
    let isRecurring = this.isRecurring;
    if (availability) {
      date = new Date(availability.startDate);
      isRecurring = availability.recurrence !== 'once';
    }
    return (index.h(index.Host, null, index.h("dvn-dialog", { title: isRecurring
        ? strings.recurringAvailability
        : calendar.CalendarUtils.format(date, 'DD MMMM dddd, YYYY'), shown: true, width: '332px', onDismiss: (e) => {
        e.stopImmediatePropagation();
        if (this.isProgressing)
          return;
        this.dismiss.emit();
      } }, this.errors.length > 0 &&
      index.h("div", { class: "form-errors-container my-2" }, this.errors.map(message => (index.h("p", { class: "formError" }, message)))), index.h("dvn-form-field", { fieldTitle: strings.recurrence, hidden: true }, index.h("dvn-select", { leaned: true, value: `${recurrence}`, disabled: true }, index.h("dvn-select-option", { value: "weekly" }, strings.weekly))), index.h("div", { class: "d-flex time-picker-container" }, index.h("dvn-form-field", { required: true, fieldTitle: strings.startTime }, index.h("dvn-time-picker", { leaned: true, hour: startHour, minute: startMinute, timeFormat: this.userSettings.timeFormat, onChange: this.onStartHourChange, disabled: isProgressing })), index.h("dvn-form-field", { required: true, fieldTitle: strings.endTime }, index.h("dvn-time-picker", { leaned: true, hour: endHour, minute: endMinute, timeFormat: this.userSettings.timeFormat, onChange: this.onEndHourChange, disabled: isProgressing }))), index.h("dvn-form-field", { required: true, fieldTitle: strings.meetingDuration }, index.h("dvn-select", { leaned: true, value: `${meetingDuration}`, onChange: e => this.meetingDuration = +e.detail, disabled: isProgressing }, index.h("dvn-select-option", { value: "15" }, "15 ", strings.minutes), index.h("dvn-select-option", { value: "30" }, "30 ", strings.minutes), index.h("dvn-select-option", { value: "45" }, "45 ", strings.minutes), index.h("dvn-select-option", { value: "60" }, "60 ", strings.minutes), index.h("dvn-select-option", { value: "90" }, "1.5 ", strings.hours), index.h("dvn-select-option", { value: "120" }, "2 ", strings.hours), index.h("dvn-select-option", { value: "150" }, "2.5 ", strings.hours), index.h("dvn-select-option", { value: "180" }, "3 ", strings.hours))), index.h("dvn-form-field", { fieldTitle: strings.breakDuration }, index.h("dvn-select", { leaned: true, value: `${breakDuration}`, onChange: e => this.breakDuration = +e.detail, disabled: isProgressing }, index.h("dvn-select-option", { value: "0" }, "-"), index.h("dvn-select-option", { value: "5" }, "5 ", strings.minutes), index.h("dvn-select-option", { value: "10" }, "10 ", strings.minutes), index.h("dvn-select-option", { value: "15" }, "15 ", strings.minutes))), index.h("dvn-form-field", { fieldTitle: strings.videoConferencePlatform, required: true }, index.h("dvn-select", { leaned: true, value: locationType, onChange: e => this.locationType = e.detail, disabled: isProgressing }, index.h("dvn-select-option", { value: "custom" }, strings.custom), index.h("dvn-select-option", { value: "jitsi" }, strings.jitsi), this.userSettings.authPlatforms.some(authPlatform => authPlatform.id === 'zoom') &&
      index.h("dvn-select-option", { value: "zoom" }, strings.zoom), this.userSettings.authPlatforms.some(authPlatform => authPlatform.id === 'google') &&
      index.h("dvn-select-option", { value: "google" }, strings.google), this.userSettings.authPlatforms.some(authPlatform => authPlatform.id === 'microsoftbusiness') &&
      index.h("dvn-select-option", { value: "microsoftbusiness" }, strings.microsoftbusiness))), this.locationType === 'custom' &&
      index.h("dvn-form-field", { fieldTitle: strings.location, required: true }, index.h("dvn-input", { leaned: true, value: locationType !== 'custom' ? '-' : location, disabled: locationType !== 'custom' || isProgressing, onChange: e => this.location = e.detail })), index.h("div", { slot: "actions" }, index.h("dvn-button", { view: "flat", onClick: () => this.dismiss.emit() }, strings.cancel), !!availability &&
      index.h("dvn-button", { view: "flat", tint: "danger", onClick: this.onClickDelete, disabled: isProgressing }, strings.deleteAvailability), index.h("dvn-button", { view: "flat", tint: "primary", onClick: this.onSubmit, disabled: isProgressing }, availability ? strings.updateAvailability : strings.createAvailability)))));
  }
};
TakvimCreateCustomAvailabilityForm.style = takvimCreateCustomAvailabilityFormCss;

exports.takvim_create_custom_availability_form = TakvimCreateCustomAvailabilityForm;
