import { r as registerInstance, c as createEvent, h, H as Host } from './index-ecdfb761.js';
import { I as InvalidationAction } from './actions-b9a0f808.js';
import './apiBase-648a161d.js';
import './data-transfer-field-2f305919.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance } from './instance-db63ecb7.js';
import { a as getEmbeddedTranslation } from './mentorinty-bridge-api-f70621dd.js';
import { g as getSessionDetails, a as mentornityApiEndpoint } from './session-a602596d.js';
import './BaseField-0b01dc2c.js';
import './FieldModelGenerator-03c7516c.js';
import './reference-field-a5b414d1.js';
import './index-61dcd269.js';
import './FieldGenerator-3ae403b2.js';
import './date-1bc489bd.js';
import './index-4e451db4.js';
import './index-32d8b974.js';
import './index-23baeeef.js';
import './index-09efdc01.js';
import './index-150b6a59.js';
import './index-5f14b4fc.js';
import './attachment-d13ad547.js';
import './data-model-f7ccf826.js';
import './membership-24db029a.js';
import './table-config-3820fb34.js';
import './string-3065a411.js';
import './User-9f1f4513.js';
import './CalendarConfig-f8d4a70d.js';
import './UserSettings-27df01e6.js';
import './cookie-9f22e0fb.js';
import './kimlik-api-66e341bd.js';
import './_commonjsHelpers-27f122dd.js';
import './index-d3bd30bd.js';
import './index-3bf9b308.js';
import './i18n-4dcb85fe.js';
import './i18n-73c2e20d.js';
import './calendar-470c802a.js';
import './index-a3ca021c.js';
import './moment-65eca6b3.js';

const MentornityFollowButton = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.action = createEvent(this, "action", 7);
    this.invalidation = 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) {
        createMentornityApiInstance(mentornityApiEndpoint, getSessionDetails().headers)
          .followUnfollowSubscription(subscription._id, isFollowing ? 'unfollow' : 'follow')
          .then(() => {
          this.invalidation.emit(new 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;
        createMentornityApiInstance(mentornityApiEndpoint, 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 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 (h(Host, { class: "d-flex" }, h("dvn-button", { disabled: this.isProgressing, onClick: this.toggleFollowStatus, tint: "primary", view: "flat" }, getEmbeddedTranslation(language, isFollowing ? 'unfollow' : 'follow'))));
  }
};

export { MentornityFollowButton as mentornity_follow_button };
