import { r as registerInstance, c as createEvent, h, H as Host } from './index-55faf181.js';
import { I as InvalidationAction } from './actions-b9a0f808.js';
import './kimlikApi-7350588e.js';
import './data-transfer-field-918730a3.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance } from './instance-46ab1e99.js';
import { g as getSessionDetails, m as mentornityApiEndpoint } from './session-3ec0aa4c.js';
import './BaseField-461b6d0c.js';
import './FieldModelGenerator-1ee7fda2.js';
import './index-6a1cf9c1.js';
import './index-403e215e.js';
import './date-ce777316.js';
import './index-083a6906.js';
import './index-fa1db470.js';
import './membership-61c4a2f7.js';
import './data-model-f5160369.js';
import './membership-config-a51a5232.js';
import './table-config-5c917348.js';
import './string-3065a411.js';
import './User-d2ee8b71.js';
import './UserSettings-7b80b841.js';
import './cookie-93ea205f.js';
import './_commonjsHelpers-27f122dd.js';
import './index-d3bd30bd.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;
    const isFollowing = ((_a = this.item.subscription) === null || _a === void 0 ? void 0 : _a.isFollowing) || false;
    return (h(Host, { class: "d-flex" }, h("dvn-button", { disabled: this.isProgressing, onClick: this.toggleFollowStatus, tint: "primary", view: "flat" }, isFollowing ? 'Unfollow' : 'Follow')));
  }
};

export { MentornityFollowButton as mentornity_follow_button };
