import { InvalidationAction } from '@dvn/core/models/actions';
import { Component, Event, h, Host, Prop, State } from '@stencil/core';
import { createMentornityApiInstance } from '../../../../mentornity-js-client';
import { getEmbeddedTranslation } from '../../api/mentorinty-bridge-api';
import { mentornityApiEndpoint } from '../../constants/env';
import { getSessionDetails } from '../../util/session';
export class MentornityFollowButton {
  constructor() {
    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'))));
  }
  static get is() { return "mentornity-follow-button"; }
  static get properties() { return {
    "item": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DataModel",
        "resolved": "DataModel",
        "references": {
          "DataModel": {
            "location": "import",
            "path": "@dvn/core/models/data-model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "data": {
      "type": "any",
      "mutable": false,
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "data",
      "reflect": false
    }
  }; }
  static get states() { return {
    "isProgressing": {},
    "isFollowing": {},
    "subscription": {}
  }; }
  static get events() { return [{
      "method": "action",
      "name": "action",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }, {
      "method": "invalidation",
      "name": "invalidation",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
}
