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-6015f41f.js';
import './data-transfer-field-2f305919.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance, g as getSessionDetails, m as mentornityApiEndpoint } from './session-2a491341.js';
import { a as getEmbeddedTranslation } from './mentorinty-bridge-api-5ca2d652.js';
import './BaseField-0b01dc2c.js';
import './membership-b6639471.js';
import './data-model-88af49fc.js';
import './table-config-d73a72ab.js';
import './FieldModelGenerator-378c8613.js';
import './reference-field-a5b414d1.js';
import './index-61dcd269.js';
import './FieldGenerator-3ae403b2.js';
import './date-1bc489bd.js';
import './index-4e451db4.js';
import './index-23baeeef.js';
import './index-09efdc01.js';
import './index-150b6a59.js';
import './string-3065a411.js';
import './User-72c4643a.js';
import './UserSettings-7e4170a7.js';
import './cookie-5d9f5819.js';
import './kimlik-api-fddeac81.js';
import './_commonjsHelpers-27f122dd.js';
import './index-d3bd30bd.js';
import './index-3bf9b308.js';
import './sozluk-entries-1e8f5234.js';
import './i18n-09a07b97.js';
import './i18n-0e757ca5.js';
import './calendar-470c802a.js';
import './index-a3ca021c.js';
import './moment-65eca6b3.js';
import './index-e66e8eca.js';
import './attachment-961dd228.js';
import './CalendarConfig-412d1b0f.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.isFollowing = false;
    this.subscription = undefined;
    this.toggleFollowStatus = () => {
      const { subscription } = this;
      this.isProgressing = true;
      if (subscription) {
        createMentornityApiInstance(mentornityApiEndpoint, getSessionDetails().headers)
          .followUnfollowSubscription(subscription._id, (this === null || this === void 0 ? void 0 : this.isFollowing) ? 'unfollow' : 'follow')
          .then(() => {
          this.isFollowing = !this.isFollowing;
          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;
        const subscriptionData = {
          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,
        };
        createMentornityApiInstance(mentornityApiEndpoint, getSessionDetails().headers)
          .createSubscription(subscriptionData)
          .then((response) => {
          this.subscription = Object.assign(Object.assign({}, subscriptionData), response);
          this.isFollowing = true;
          this.invalidation.emit(new InvalidationAction('mentornity-follow-button', 'mentornity-user-detail-page'));
        }).finally(() => this.isProgressing = false);
      }
    };
  }
  componentWillLoad() {
    var _a;
    this.subscription = this.item.subscription;
    this.isFollowing = ((_a = this.item.subscription) === null || _a === void 0 ? void 0 : _a.isFollowing) || false;
  }
  render() {
    var _a, _b;
    const isOnUserList = ((_a = this.data) === null || _a === void 0 ? void 0 : _a.isOnUserList) || false;
    const language = ((_b = this.data) === null || _b === void 0 ? void 0 : _b.language) || 'en';
    return (h(Host, { class: isOnUserList ? 'd-inline' : 'd-flex' }, h("dvn-button", { disabled: this.isProgressing, onClick: this.toggleFollowStatus, tint: "primary", view: this.isFollowing ? 'outline' : 'solid', scale: isOnUserList ? 'small' : undefined, leaned: true }, getEmbeddedTranslation(language, this.isFollowing ? 'unfollow' : 'follow'))));
  }
};

export { MentornityFollowButton as mentornity_follow_button };
