'use strict';

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

const index = require('./index-1141a245.js');
const actions = require('./actions-d7eabd95.js');
require('./apiBase-e4ebaf8b.js');
require('./data-transfer-field-e8417d5f.js');
require('./filter-d5040f65.js');
const instance = require('./instance-36d7564f.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-0534712d.js');
const session = require('./session-b6f20e12.js');
require('./BaseField-381327a9.js');
require('./FieldModelGenerator-1900d81a.js');
require('./reference-field-1239b0c3.js');
require('./index-2eea69a0.js');
require('./FieldGenerator-279a646b.js');
require('./date-a24d7714.js');
require('./index-0783d14f.js');
require('./index-e8e4d80b.js');
require('./index-9d3d3d15.js');
require('./index-9fd84b93.js');
require('./index-61f9621a.js');
require('./index-e751cab0.js');
require('./attachment-b0acc4c1.js');
require('./data-model-9afb7f54.js');
require('./membership-ed8d3277.js');
require('./table-config-8b14c7d3.js');
require('./string-de672b69.js');
require('./User-e889279b.js');
require('./CalendarConfig-ee7bb722.js');
require('./UserSettings-fc04eb54.js');
require('./cookie-135bf612.js');
require('./kimlik-api-d3c479a3.js');
require('./_commonjsHelpers-206db00d.js');
require('./index-33e96540.js');
require('./index-a2e99c85.js');
require('./i18n-49870fc9.js');
require('./i18n-5585a319.js');
require('./calendar-e4b7baeb.js');
require('./index-b776fc14.js');
require('./moment-7a3433f2.js');

const MentornityFollowButton = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.invalidation = index.createEvent(this, "invalidation", 7);
    this.isProgressing = false;
    this.toggleFollowStatus = () => {
      const { subscription } = this.item;
      const isFollowing = (subscription === null || subscription === void 0 ? void 0 : subscription.isFollowing) || false;
      this.isProgressing = true;
      if (subscription) {
        instance.createMentornityApiInstance(session.mentornityApiEndpoint, session.getSessionDetails().headers)
          .followUnfollowSubscription(subscription._id, isFollowing ? 'unfollow' : 'follow')
          .then(() => {
          this.invalidation.emit(new actions.InvalidationAction('mentornity-follow-button', 'mentornity-user-detail-page'));
        }).finally(() => this.isProgressing = false);
      }
      else {
        const { mentornityCookieContext: { groupId, userId } } = this.data;
        const isMenteeGroup = !!groupId;
        const { mentorship } = this.item;
        const { organisation } = mentorship;
        instance.createMentornityApiInstance(session.mentornityApiEndpoint, session.getSessionDetails().headers)
          .createSubscription({
          organisation: { _type: 'reference', _class: 'organisations', _id: organisation._id },
          subscribed: {
            _type: 'reference',
            _class: 'users',
            _id: userId,
          },
          subscriber: {
            _type: 'reference',
            _class: isMenteeGroup ? 'groups' : 'users',
            _id: isMenteeGroup ? groupId : userId,
          },
          createdBy: { _type: 'reference', _class: 'users', _id: userId },
          mentorship: { _type: 'reference', _class: 'mentorships', _id: mentorship._id },
          status: 'active',
          isFollowing: true,
        })
          .then(() => {
          this.invalidation.emit(new actions.InvalidationAction('mentornity-follow-button', 'mentornity-user-detail-page'));
        }).finally(() => this.isProgressing = false);
      }
    };
  }
  render() {
    var _a, _b;
    const isFollowing = ((_a = this.item.subscription) === null || _a === void 0 ? void 0 : _a.isFollowing) || false;
    const language = ((_b = this.data) === null || _b === void 0 ? void 0 : _b.language) || 'en';
    return (index.h(index.Host, { class: "d-flex" }, index.h("dvn-button", { disabled: this.isProgressing, onClick: this.toggleFollowStatus, tint: "primary", view: "flat" }, mentorintyBridgeApi.getEmbeddedTranslation(language, isFollowing ? 'unfollow' : 'follow'))));
  }
};

exports.mentornity_follow_button = MentornityFollowButton;
