'use strict';

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

const index = require('./index-1141a245.js');
const actions = require('./actions-d7eabd95.js');
require('./apiBase-6c66ff1b.js');
require('./data-transfer-field-e8417d5f.js');
require('./filter-d5040f65.js');
const session = require('./session-f588baa3.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-b1eaa56d.js');
require('./BaseField-381327a9.js');
require('./membership-e87e02ed.js');
require('./data-model-01c368bc.js');
require('./table-config-969d3038.js');
require('./FieldModelGenerator-d61f01d4.js');
require('./reference-field-1239b0c3.js');
require('./index-2eea69a0.js');
require('./FieldGenerator-279a646b.js');
require('./date-a24d7714.js');
require('./index-0783d14f.js');
require('./index-9d3d3d15.js');
require('./index-9fd84b93.js');
require('./index-61f9621a.js');
require('./string-de672b69.js');
require('./User-dbfbd45e.js');
require('./UserSettings-2a2399b1.js');
require('./cookie-e64bc800.js');
require('./kimlik-api-663f4884.js');
require('./_commonjsHelpers-206db00d.js');
require('./index-33e96540.js');
require('./index-a2e99c85.js');
require('./sozluk-entries-3b9686e7.js');
require('./i18n-0bcf144d.js');
require('./i18n-77d45414.js');
require('./calendar-e4b7baeb.js');
require('./index-b776fc14.js');
require('./moment-7a3433f2.js');
require('./index-b7855ed6.js');
require('./attachment-a0cdf203.js');
require('./CalendarConfig-998f742e.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.isFollowing = false;
    this.subscription = undefined;
    this.toggleFollowStatus = () => {
      const { subscription } = this;
      this.isProgressing = true;
      if (subscription) {
        session.createMentornityApiInstance(session.mentornityApiEndpoint, session.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 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;
        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,
        };
        session.createMentornityApiInstance(session.mentornityApiEndpoint, session.getSessionDetails().headers)
          .createSubscription(subscriptionData)
          .then((response) => {
          this.subscription = Object.assign(Object.assign({}, subscriptionData), response);
          this.isFollowing = true;
          this.invalidation.emit(new actions.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 (index.h(index.Host, { class: isOnUserList ? 'd-inline' : 'd-flex' }, index.h("dvn-button", { disabled: this.isProgressing, onClick: this.toggleFollowStatus, tint: "primary", view: this.isFollowing ? 'outline' : 'solid', scale: isOnUserList ? 'small' : undefined, leaned: true }, mentorintyBridgeApi.getEmbeddedTranslation(language, this.isFollowing ? 'unfollow' : 'follow'))));
  }
};

exports.mentornity_follow_button = MentornityFollowButton;
