import { r as registerInstance, h, H as Host } from './index-55faf181.js';
import { i as injectContext } from './index-8d5c9a8e.js';
import './context-8273b228.js';
import './state-tunnel-04c0b67a.js';

function wrapContent(content) {
  return (h(Host, { class: "row justify-content-center mt-3" }, content));
}
const DvnAppMembershipConsent = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.agreements = [];
    this.consentedAgreements = [];
    this.onAgree = async (agreement) => {
      const hasConsent = this.consentedAgreements.some(a => a._id === agreement._id);
      if (hasConsent) {
        this.consentedAgreements = this.consentedAgreements.filter(a => a._id !== agreement._id);
      }
      else {
        this.consentedAgreements = [...this.consentedAgreements, agreement];
      }
    };
    this.onSubmit = async () => {
      const { consentedAgreements } = this;
      let promises = [];
      for (let i = 0; i < consentedAgreements.length; i++) {
        promises = [...promises, this.apiUye.consentAgreement(consentedAgreements[i]._id)];
      }
      const consents = await Promise.all(promises).catch(error => this.error = error);
      if (!consents)
        return;
      const result = await this.apiUye.updateMembership(this.membership.config._id, this.membership._id, { consents })
        .catch(error => this.error = error);
      if (!result)
        return;
      this.appContextInvalidator();
    };
  }
  componentDidLoad() {
    const { agreements } = this.membership.config;
    if (this.agreements.length >= agreements.length) {
      return;
    }
    for (let i = 0; i < agreements.length; i++) {
      this.apiUye.getAgreement(agreements[i]._id).then(response => this.agreements = [...this.agreements, response], error => this.error = error);
    }
  }
  render() {
    var _a, _b;
    const { agreements, consentedAgreements, membership } = this;
    const { config } = membership;
    if (agreements.length !== config.agreements.length) {
      return wrapContent(h("div", { class: "col-4 d-flex flex-column justify-content-center align-items-center mt-3" }, h("div", { class: "d-flex justify-content-center" }, h("dvn-progress", { indefinite: true }))));
    }
    return wrapContent(h("div", { class: "col-sm-12 col-md-8" }, h("div", { class: "d-flex flex-column" }, h("dvn-app-space-message-card", { space: membership.config.space }, membership.config.space.name, " requires you to read and accept these terms and agreements."), agreements.map((agreement, i) => {
      const hasConsent = consentedAgreements.some(a => a._id === agreement._id);
      return (h("dvn-card", { leaned: i % 2 === 0, leanedSides: true, elevation: 0 }, h("dvn-card-header", { title: agreement.title, slot: "header" }, h("div", { slot: "action" }, h("dvn-button", { onClick: () => this.selectedAgreement = agreement, scale: "small", view: "outline" }, "Read Agreement"))), h("dvn-checkbox", { leaned: true, checked: hasConsent, label: "I read and accept the terms in the condition", onChange: () => this.onAgree(agreement) })));
    }), h("dvn-button", { fill: true, disabled: consentedAgreements.length !== agreements.length, leaned: true, onClick: () => this.onSubmit(), tint: "primary" }, "Give Consent"), h("dvn-dialog", { title: (_a = this.selectedAgreement) === null || _a === void 0 ? void 0 : _a.title, shown: !!this.selectedAgreement, onDismiss: () => this.selectedAgreement = undefined }, h("dvn-markdown", { content: (_b = this.selectedAgreement) === null || _b === void 0 ? void 0 : _b.content })))));
  }
};
injectContext(DvnAppMembershipConsent);

export { DvnAppMembershipConsent as dvn_app_membership_consent };
