'use strict';

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

const index = require('./index-ce58ddc5.js');
const ui = require('./ui-2403d3b4.js');
require('./index-fa185e3b.js');
require('./index-f92537c9.js');
const referenceField = require('./reference-field-dda66f2e.js');
const index$1 = require('./index-7e3bc620.js');
const appConfig = require('./app-config-cb7e78a4.js');
const i18n = require('./i18n-9a433e64.js');
const date = require('./date-25b104f6.js');
const attachment = require('./attachment-3090e25e.js');
require('./BaseField-d019ade6.js');
require('./data-model-9afb7f54.js');

const atamaStructuredPairOverviewCss = "atama-structured-pair-overview{display:block}";

let AtamaStructuredPairOverview = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.meetingsByIdentifiers = {};
  }
  async invalidate(_event) {
    this.fetchMeetings();
  }
  ;
  componentDidLoad() {
    this.fetchMeetings();
  }
  async fetchMeetings() {
    var _a;
    const { pairConfig, userId, sessionUserId } = this;
    const response = await this.apiTakvim.getMeetingsOfUser((_a = pairConfig.calendarConfig) === null || _a === void 0 ? void 0 : _a._id, [sessionUserId, userId]);
    const meetingsByIdentifiers = {};
    response.results.forEach(meeting => {
      meetingsByIdentifiers[meeting.meetingType] = meeting;
    });
    this.meetingsByIdentifiers = meetingsByIdentifiers;
  }
  render() {
    const { shownMeetingConfig, size, userRefFieldId } = this;
    const showMobileUI = size < ui.ResponsiveSize.Medium;
    return (index.h(index.Host, { class: "mb-3" }, showMobileUI
      ? this.renderList()
      : index.h("dvn-card", { leaned: true, bodyLeaned: true, elevation: 0 }, this.renderList()), index.h("dvn-dialog", { title: shownMeetingConfig ? shownMeetingConfig.name : undefined, shown: !!shownMeetingConfig, onDismiss: () => this.shownMeetingConfig = undefined }, shownMeetingConfig &&
      index.h("dvn-markdown", { content: shownMeetingConfig.specifications[userRefFieldId].description }))));
  }
  renderList() {
    const { i18n, meetingsByIdentifiers, pairConfig, size } = this;
    const showMobileUI = size < ui.ResponsiveSize.Medium;
    const Tag = showMobileUI
      ? 'atama-structured-pair-overview-meeting-row-intense'
      : 'atama-structured-pair-overview-meeting-row';
    return pairConfig.structure.meetingConfigs.map((meetingConfig, index$1) => {
      const meeting = meetingsByIdentifiers[meetingConfig.id];
      const nextMeetingGoal = pairConfig.structure.meetingConfigs.length > index$1 + 2
        && pairConfig.structure.meetingConfigs[index$1 + 1];
      const nextCompleted = nextMeetingGoal && !!meetingsByIdentifiers[nextMeetingGoal.id];
      return (index.h(Tag, { i18n: i18n, meeting: meeting, meetingConfig: meetingConfig, meetingOrder: index$1 + 1, nextCompleted: nextCompleted, onCreateMeeting: (config) => this.action.emit({
          name: 'create-meeting-with-config-only',
          data: {
            meetingConfig: config,
          }
        }), onShowMeetingDetail: (meetingToShow) => {
          this.action.emit({
            name: 'open-meeting-detail',
            data: {
              meeting: meetingToShow,
            }
          });
        }, onShowMeetingConfigDetail: (config) => {
          this.shownMeetingConfig = config;
        } }));
    });
  }
};
AtamaStructuredPairOverview.style = atamaStructuredPairOverviewCss;

const takvimMeetingListCss = "takvim-meeting-list{display:block}takvim-meeting-list .progress-container{display:-ms-flexbox;display:flex;height:100%;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;padding-bottom:1rem}";

const collectionFields = [
  new date.DateField('startDate', 'Date').setDateFormat('DD MMM YYYY'),
  new referenceField.ReferenceField('guests', 'Guests', 'name', 'imageUrl', 'users', [
    new attachment.AttachmentField('imageUrl'),
    new index$1.TextField('name', ''),
  ], false)
];
const listViewConfig = (userSettings, showMobileUI) => {
  const { timeFormat, dateFormat } = userSettings;
  const hourFormat = timeFormat === '12-hour' ? 'hh:mm A' : 'HH:mm';
  const formattedDate = showMobileUI ? (dateFormat === 'DD/MM/YYYY' ? 'DD MMM' : 'MMM DD') : (dateFormat === 'DD/MM/YYYY' ? 'DD MMMM YYYY, ddd' : 'MMMM DD YYYY, ddd');
  const fields = [
    {
      fieldId: 'startDate',
      fieldDisplay: new date.DateField('startDate', 'Date').setDateFormat(formattedDate),
      timeZone: userSettings.timeZone,
    },
    {
      fieldId: 'startHour',
      fieldDisplay: new date.DateField('startDate', 'Start').setDateFormat(hourFormat),
      timeZone: userSettings.timeZone,
    },
    {
      fieldId: 'endHour',
      fieldDisplay: new date.DateField('endDate', 'End').setDateFormat(hourFormat),
      timeZone: userSettings.timeZone,
    },
  ];
  if (!showMobileUI) {
    fields.push({ fieldId: 'guests', imageShape: 'circle' });
  }
  return {
    hideHeader: true,
    actionsWidth: showMobileUI ? 48 : 64,
    actions: [
      {
        ui: {
          name: 'open-meeting-detail-from-meeting-list',
          label: 'Open',
          buttonType: 'icon-button',
          iconName: 'uil uil-arrow-right',
          view: 'flat',
        },
        data: {
          type: 'navigation',
          direction: 'forward',
          placement: appConfig.ViewPlacement.Page,
          viewId: 'meeting-page',
          params: {
            conversionConfig: {
              'item._id': 'meetingId',
            },
          }
        }
      },
    ],
    fields,
    columns: {
      'startHour': showMobileUI ? { weight: 1 } : { width: 100 },
      'guests': {
        width: 100
      }
    }
  };
};
let TakvimMeetingList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.isProgressing = true;
    this.strings = i18n.takvimMeetingListI18nDefault;
    this.onSizeChange = (size) => {
      this.size = size;
    };
  }
  componentWillLoad() {
    this.uiHelper = new ui.ResponsiveUIHelper(this.el, this.onSizeChange);
    this.size = ui.ResponsiveUIHelper.getSize(this.el.parentElement.clientWidth);
  }
  componentDidLoad() {
    this.fetchMeetings();
  }
  async invalidate(_event) {
    this.meetings = undefined;
    this.error = undefined;
    this.fetchMeetings();
  }
  ;
  async fetchMeetings() {
    const { calendarConfigId, guestIDs } = this;
    const meetings = await this.apiTakvim.getMeetingsOfUser(calendarConfigId, guestIDs).catch(e => {
      this.error = e;
    }).finally(() => this.isProgressing = false);
    if (!meetings)
      return;
    this.meetings = meetings.results;
  }
  render() {
    const { meetings, size, strings, userSettings } = this;
    if (!meetings) {
      return (index.h(index.Host, { class: "mb-3" }, index.h("div", { class: "progress-container" }, index.h("dvn-progress", { indefinite: true, tint: "primary" }))));
    }
    const showMobileUI = size < ui.ResponsiveSize.Small;
    return (index.h(index.Host, { class: "mb-3" }, meetings.length === 0 &&
      index.h("dvn-card", { elevation: 0, leaned: true }, index.h("div", { class: "empty-message-container text-centered" }, strings.noItemInList)), index.h("dvn-list-view-table", { config: listViewConfig(userSettings, showMobileUI), fields: collectionFields, items: meetings, isEndReached: true, isProgressing: this.isProgressing, error: this.error ? strings.fetchingListFailed : undefined, onRetry: () => this.fetchMeetings(), params: {} })));
  }
  get el() { return index.getElement(this); }
};
TakvimMeetingList.style = takvimMeetingListCss;

exports.atama_structured_pair_overview = AtamaStructuredPairOverview;
exports.takvim_meeting_list = TakvimMeetingList;
