import { r as registerInstance, c as createEvent, h, H as Host } from './index-6vpKbd31.js';
import { VerdocsEndpoint, getEnvelope, getInPersonLink, getRecipientsWithActions, recipientCanAct, formatFullName } from '@verdocs/js-sdk';
import { S as SDKError } from './errors-C1sRVZWv.js';
import { S as Store } from './Datastore-DmsV595e.js';

const verdocsEnvelopeRecipientSummaryCss = "@-webkit-keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}@keyframes verdocs-field-pulse{0%{background-color:rgba(0, 0, 0, 0.35)}50%{background-color:rgba(0, 0, 0, 0)}100%{background-color:rgba(0, 0, 0, 0.35)}}verdocs-envelope-recipient-summary{top:0;left:0;right:0;bottom:0;display:-ms-flexbox;display:flex;z-index:100000;position:fixed;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;font-family:\"Inter\", \"Barlow\", sans-serif;background-color:rgba(0, 0, 0, 0.4980392157)}verdocs-envelope-recipient-summary .summary-content{width:600px;display:-ms-flexbox;display:flex;max-width:90%;font-size:18px;-ms-flex:0 0 600px;flex:0 0 600px;border-radius:6px;background:#ffffff;-ms-flex-direction:column;flex-direction:column;padding:30px 20px 20px}verdocs-envelope-recipient-summary .summary-content .summary-title{font-size:20px;font-weight:700;margin:0 0 10px 0;color:#33364b}verdocs-envelope-recipient-summary .summary-recipient{display:-ms-flexbox;display:flex;color:#33364b;margin:8px 0 24px 0;-ms-flex-direction:column;flex-direction:column}verdocs-envelope-recipient-summary .summary-recipient .role-name{-ms-flex:1;flex:1;display:-ms-flexbox;display:flex;font-size:14px;font-weight:600;margin:0 0 8px 0;color:#092c4c}verdocs-envelope-recipient-summary .summary-recipient .role-info{gap:5px;display:-ms-flexbox;display:flex;-ms-flex-direction:row;flex-direction:row;-ms-flex-align:end;align-items:flex-end}verdocs-envelope-recipient-summary .summary-recipient .role-full-name{-ms-flex:1;flex:1;height:34px;display:-ms-flexbox;display:flex;font-size:16px;padding:0 12px;border-radius:4px;-ms-flex-align:center;align-items:center;border:1px solid #cccccc;background:rgb(249, 250, 251)}verdocs-envelope-recipient-summary .summary-recipient .recipient-link{width:120px;padding:0 16px;font-weight:500;margin:0 0 0 8px;text-align:center;font-size:0.875rem;border-radius:100%;color:rgb(55, 65, 81);line-height:1.25rem;background-color:#d8d8d8;border:2px solid rgb(156, 163, 175)}verdocs-envelope-recipient-summary .summary-recipient .link-wrapper{gap:5px;display:-ms-flexbox;display:flex;margin:4px 0 0 0;-ms-flex-direction:row;flex-direction:row}verdocs-envelope-recipient-summary .summary-recipient .link-wrapper .link{-ms-flex:1;flex:1;height:34px;display:-ms-flexbox;display:flex;font-size:16px;padding:0 12px;overflow:hidden;border-radius:4px;-ms-flex-align:center;align-items:center;white-space:nowrap;text-overflow:ellipsis;border:1px solid #cccccc;background:rgb(249, 250, 251)}verdocs-envelope-recipient-summary .buttons{display:-ms-flexbox;display:flex;-webkit-column-gap:15px;-moz-column-gap:15px;column-gap:15px;margin:10px 0 0 0;-ms-flex-direction:row;flex-direction:row;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center}verdocs-envelope-recipient-summary .buttons verdocs-button{-ms-flex:0 0 120px;flex:0 0 120px;margin-left:10px;white-space:nowrap}verdocs-envelope-recipient-summary .buttons verdocs-button button{width:100%}";

const VerdocsEnvelopeRecipientSummary = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.another = createEvent(this, "another");
        this.view = createEvent(this, "view");
        this.next = createEvent(this, "next");
        this.sdkError = createEvent(this, "sdkError");
        this.envelopeListenerId = null;
        /**
         * The endpoint to use to communicate with Verdocs. If not set, the default endpoint will be used.
         */
        this.endpoint = VerdocsEndpoint.getDefault();
        /**
         * The envelope ID to edit.
         */
        this.envelopeId = '';
        /**
         * Enable or disable the Send Another button.
         */
        this.canSendAnother = true;
        /**
         * Enable or disable the View button.
         */
        this.canView = true;
        /**
         * Enable or disable the Done button.
         */
        this.canDone = true;
        this.recipientStatusIcons = [];
        this.containerId = `verdocs-status-indicator-${Math.random().toString(36).substring(2, 11)}`;
        this.gettingLinks = {};
        this.links = {};
        this.loading = true;
        this.envelope = null;
    }
    async componentWillLoad() {
        var _a, _b, _c;
        try {
            this.endpoint.loadSession();
            if (!this.envelopeId) {
                console.log(`[RECIPIENTS] Missing required envelope ID ${this.envelopeId}`);
                return;
            }
            if (!this.endpoint.session) {
                console.log('[RECIPIENTS] Unable to start session, must be authenticated');
                return;
            }
            this.listenToEnvelope();
        }
        catch (e) {
            console.log('[RECIPIENTS] Error loading envelope', e);
            (_a = this.sdkError) === null || _a === void 0 ? void 0 : _a.emit(new SDKError(e.message, (_b = e.response) === null || _b === void 0 ? void 0 : _b.status, (_c = e.response) === null || _c === void 0 ? void 0 : _c.data));
        }
    }
    disconnectedCallback() {
        this.unlistenToEnvelope();
    }
    async listenToEnvelope() {
        console.log('[SIDEBAR] Loading envelope', this.envelopeId);
        this.unlistenToEnvelope();
        Store.subscribe('envelopes', this.envelopeId, () => getEnvelope(this.endpoint, this.envelopeId), false, (envelope) => {
            this.envelope = envelope;
            this.loading = false;
            this.sortEnvelopeRecipients();
        });
    }
    unlistenToEnvelope() {
        if (this.envelopeListenerId) {
            Store.store.delListener(this.envelopeListenerId);
            this.envelopeListenerId = null;
        }
    }
    sortEnvelopeRecipients() {
        var _a;
        (((_a = this.envelope) === null || _a === void 0 ? void 0 : _a.recipients) || []).sort((a, b) => {
            return a.sequence === b.sequence ? a.order - b.order : a.sequence - b.sequence;
        });
    }
    handleAnother(e) {
        var _a;
        e.preventDefault();
        (_a = this.another) === null || _a === void 0 ? void 0 : _a.emit({ envelope: this.envelope });
    }
    handleView(e) {
        var _a;
        e.preventDefault();
        (_a = this.view) === null || _a === void 0 ? void 0 : _a.emit({ envelope: this.envelope });
    }
    handleDone(e) {
        var _a;
        e.preventDefault();
        (_a = this.next) === null || _a === void 0 ? void 0 : _a.emit({ envelope: this.envelope });
    }
    copyLink(link) {
        navigator.clipboard
            .writeText(link)
            .then(() => {
            // VerdocsToast('Link copied to clipboard!', {style: 'success', duration: 3000});
        })
            .catch(e => {
            var _a;
            console.warn('[RECIPIENTS] Error copying to clipboard', e);
            (_a = this.sdkError) === null || _a === void 0 ? void 0 : _a.emit(e);
            // VerdocsToast(`Unable to copy to clipboard: ${e.message}`, {style: 'error'});
        });
    }
    getLink(recipient) {
        this.gettingLinks = { ...this.gettingLinks, [recipient.role_name]: true };
        getInPersonLink(this.endpoint, recipient.envelope_id, recipient.role_name)
            .then(response => {
            this.gettingLinks = { ...this.gettingLinks, [recipient.role_name]: false };
            this.links = { ...this.links, [recipient.role_name]: response.link };
            this.copyLink(response.link);
        })
            .catch(e => {
            var _a;
            this.gettingLinks = { ...this.gettingLinks, [recipient.role_name]: false };
            console.log('[RECIPIENTS] Error getting link', e);
            (_a = this.sdkError) === null || _a === void 0 ? void 0 : _a.emit(e);
            // VerdocsToast('Unable to get link: ' + e.message, {style: 'error'});
        });
    }
    render() {
        var _a;
        if (this.loading) {
            return h(Host, null);
        }
        return (h(Host, null, h("div", { class: "summary-content" }, h("h1", { class: "summary-title" }, "Recipient Summary"), h("div", { class: "summary-rows" }, (((_a = this.envelope) === null || _a === void 0 ? void 0 : _a.recipients) || []).map(recipient => {
            const recipientsWithActions = getRecipientsWithActions(this.envelope);
            const showLinkButton = recipientCanAct(recipient, recipientsWithActions);
            const link = this.links[recipient.role_name];
            const gettingLink = this.gettingLinks[recipient.role_name];
            const fullName = formatFullName(recipient);
            return (h("div", { class: "summary-recipient" }, h("div", { class: "role-name" }, recipient.role_name), h("div", { class: "role-details" }, h("div", { class: "role-info" }, h("div", { class: "role-full-name" }, fullName, " (", recipient.email || recipient.phone, ")"), showLinkButton && !link && h("verdocs-button", { size: "small", variant: "outline", label: "Get Link", disabled: gettingLink, onClick: () => this.getLink(recipient) })), link && (h("div", { class: "link-wrapper" }, h("div", { class: "link" }, link), h("verdocs-button", { size: "small", variant: "outline", label: "Copy", onClick: () => this.copyLink(link) }))))));
        })), (this.canSendAnother || this.canView || this.canDone) && (h("div", { class: "buttons" }, this.canSendAnother && h("verdocs-button", { size: "small", label: "Send Another", onClick: e => this.handleAnother(e) }), this.canView && h("verdocs-button", { size: "small", label: "View Now", onClick: e => this.handleView(e) }), this.canDone && h("verdocs-button", { size: "small", label: "Done", onClick: e => this.handleDone(e) }))))));
    }
};
VerdocsEnvelopeRecipientSummary.style = verdocsEnvelopeRecipientSummaryCss;

export { VerdocsEnvelopeRecipientSummary as verdocs_envelope_recipient_summary };
//# sourceMappingURL=verdocs-envelope-recipient-summary.entry.js.map

//# sourceMappingURL=verdocs-envelope-recipient-summary.entry.js.map