'use strict';

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

const index = require('./index-1ee80297.js');
const actions = require('./actions-d7eabd95.js');
require('./kimlikApi-1cd43caf.js');
require('./data-transfer-field-0ce05c1a.js');
require('./filter-d5040f65.js');
const instance = require('./instance-8904b0e4.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-ede51458.js');
const env = require('./env-56a21d68.js');
const session = require('./session-612c0d2f.js');
require('./BaseField-d019ade6.js');
require('./FieldModelGenerator-2c39c8e9.js');
require('./index-42baafa9.js');
require('./index-a77c3004.js');
require('./date-25b104f6.js');
require('./index-521d07b9.js');
require('./index-9e7c4ca0.js');
require('./index-30008542.js');
require('./attachment-3090e25e.js');
require('./data-model-c51de090.js');
require('./membership-14c44e7c.js');
require('./membership-config-6ba394cc.js');
require('./table-config-be88299b.js');
require('./string-de672b69.js');
require('./User-286a0714.js');
require('./CalendarConfig-fed039db.js');
require('./UserSettings-016122e8.js');
require('./cookie-c4d9848a.js');
require('./_commonjsHelpers-206db00d.js');
require('./index-33e96540.js');

let 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(env.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(env.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, _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 (index.h(index.Host, { class: "d-flex" }, index.h("dvn-button", { disabled: this.isProgressing, onClick: this.toggleFollowStatus, tint: "primary", view: "flat" }, mentorintyBridgeApi.getEmbeddedTranslation(language, isFollowing ? 'unfollow' : 'follow'))));
  }
};

exports.mentornity_follow_button = MentornityFollowButton;
