'use strict';

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

const index = require('./index-36b3af04.js');
const actions = require('./actions-d7eabd95.js');
require('./kimlikApi-85c6d3ac.js');
require('./data-transfer-field-0ce05c1a.js');
require('./filter-d5040f65.js');
const instance = require('./instance-79d61640.js');
const session = require('./session-3b788cd7.js');
require('./BaseField-d019ade6.js');
require('./FieldModelGenerator-205dd99c.js');
require('./index-74a00556.js');
require('./index-a8f1783c.js');
require('./date-25b104f6.js');
require('./index-fe19a98b.js');
require('./index-ae305bae.js');
require('./membership-dfbf512e.js');
require('./data-model-c51de090.js');
require('./membership-config-707b2c51.js');
require('./table-config-444a77e4.js');
require('./string-de672b69.js');
require('./User-286a0714.js');
require('./UserSettings-016122e8.js');
require('./cookie-370bc992.js');
require('./_commonjsHelpers-206db00d.js');
require('./index-33e96540.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;
    const isFollowing = ((_a = this.item.subscription) === null || _a === void 0 ? void 0 : _a.isFollowing) || false;
    return (index.h(index.Host, { class: "d-flex" }, index.h("dvn-button", { disabled: this.isProgressing, onClick: this.toggleFollowStatus, tint: "primary", view: "flat" }, isFollowing ? 'Unfollow' : 'Follow')));
  }
};

exports.mentornity_follow_button = MentornityFollowButton;
