import { r as registerInstance, c as createEvent, h, H as Host } from './index-b7735e0d.js';
import { I as InvalidationAction } from './actions-b9a0f808.js';
import './apiBase-d573f009.js';
import './data-transfer-field-918730a3.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance } from './instance-7b02ef1d.js';
import { g as getEmbeddedTranslation } from './mentorinty-bridge-api-78ab1153.js';
import { m as mentornityApiEndpoint } from './env-4ec3c807.js';
import { g as getSessionDetails } from './session-b16a440c.js';
import './BaseField-461b6d0c.js';
import './FieldModelGenerator-020f4a04.js';
import './reference-field-0e7cd534.js';
import './index-7b119e61.js';
import './index-403101cb.js';
import './date-ce777316.js';
import './index-1d3efe49.js';
import './index-eb14fd2d.js';
import './index-80ec23ef.js';
import './index-4c39b6a6.js';
import './index-774783ee.js';
import './attachment-32decf79.js';
import './data-model-f7ccf826.js';
import './membership-e92743d1.js';
import './table-config-0c06fb1f.js';
import './string-3065a411.js';
import './User-1ef5a482.js';
import './CalendarConfig-88c2213e.js';
import './UserSettings-27df01e6.js';
import './cookie-1686830b.js';
import './kimlikApi-0d7e6ce7.js';
import './_commonjsHelpers-27f122dd.js';
import './index-d3bd30bd.js';

let 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 };
