import { h, Host } from "@stencil/core";
import { getEnvelope, VerdocsEndpoint } from "@verdocs/js-sdk";
import { SDKError } from "../../../utils/errors";
import { Store } from "../../../utils/Datastore";
const QuestionIcon = `<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M10.4809 13.8423H15.4C16.2962 13.8423 17 13.1288 17 12.2764V5.56582C17 4.71348 16.2962 4 15.4 4H4.6C3.70383 4 3 4.71348 3 5.56582V12.2764C3 13.1288 3.70383 13.8423 4.6 13.8423H6.19908L6.2 17L6.20346 16.9997L6.20502 16.9988L10.4809 13.8423ZM6.79895 17.8034C6.35668 18.1298 5.73 18.0406 5.39921 17.6042C5.26989 17.4335 5.2 17.2262 5.2 17.0133L5.19937 14.8423H4.6C3.16406 14.8423 2 13.6935 2 12.2764V5.56582C2 4.14876 3.16406 3 4.6 3H15.4C16.8359 3 18 4.14876 18 5.56582V12.2764C18 13.6935 16.8359 14.8423 15.4 14.8423H10.81L6.79895 17.8034Z" fill="#424242"/></svg>`;
const DeclineIcon = `<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M7.14645 7.14645C7.34171 6.95118 7.65829 6.95118 7.85355 7.14645L10 9.29289L12.1464 7.14645C12.3417 6.95118 12.6583 6.95118 12.8536 7.14645C13.0488 7.34171 13.0488 7.65829 12.8536 7.85355L10.7071 10L12.8536 12.1464C13.0488 12.3417 13.0488 12.6583 12.8536 12.8536C12.6583 13.0488 12.3417 13.0488 12.1464 12.8536L10 10.7071L7.85355 12.8536C7.65829 13.0488 7.34171 13.0488 7.14645 12.8536C6.95118 12.6583 6.95118 12.3417 7.14645 12.1464L9.29289 10L7.14645 7.85355C6.95118 7.65829 6.95118 7.34171 7.14645 7.14645ZM3 6C3 4.34315 4.34315 3 6 3H14C15.6569 3 17 4.34315 17 6V14C17 15.6569 15.6569 17 14 17H6C4.34315 17 3 15.6569 3 14V6ZM6 4C4.89543 4 4 4.89543 4 6V14C4 15.1046 4.89543 16 6 16H14C15.1046 16 16 15.1046 16 14V6C16 4.89543 15.1046 4 14 4H6Z" fill="#424242"/></svg>`;
const SaveIcon = `<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M3 5C3 3.89543 3.89543 3 5 3H13.3787C13.9091 3 14.4178 3.21071 14.7929 3.58579L16.4142 5.20711C16.7893 5.58218 17 6.09089 17 6.62132V15C17 16.1046 16.1046 17 15 17H5C3.89543 17 3 16.1046 3 15V5ZM5 4C4.44772 4 4 4.44772 4 5V15C4 15.5523 4.44772 16 5 16L5 11.5C5 10.6716 5.67157 10 6.5 10H13.5C14.3284 10 15 10.6716 15 11.5V16C15.5523 16 16 15.5523 16 15V6.62132C16 6.3561 15.8946 6.10175 15.7071 5.91421L14.0858 4.29289C13.8983 4.10536 13.6439 4 13.3787 4L13 4V6.5C13 7.32843 12.3284 8 11.5 8L7.5 8C6.67157 8 6 7.32843 6 6.5L6 4H5ZM7 4L7 6.5C7 6.77614 7.22386 7 7.5 7L11.5 7C11.7761 7 12 6.77614 12 6.5V4L7 4ZM14 16V11.5C14 11.2239 13.7761 11 13.5 11H6.5C6.22386 11 6 11.2239 6 11.5V16H14Z" fill="#424242"/></svg>`;
/**
 * Typically presented by the `verdocs-sign` component. Displays a footer toolbar
 * with a few convenience functions for the envelope recipient to use.
 */
export class VerdocsSignFooter {
    constructor() {
        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 render. Set ONE OF templateId or envelopeId. If both are set, envelopeId will be ignored.
         */
        this.envelopeId = '';
        /**
         * If the recipient is "done," some buttons will be hidden.
         */
        this.isDone = false;
        /**
         * Event fired when the envelope is updated in any way. May be used for tasks such as cache invalidation or reporting to other systems.
         */
        // @Event({composed: true}) envelopeUpdated: EventEmitter<{endpoint: VerdocsEndpoint; envelope: IEnvelope; event: string}>;
        this.askingQuestion = false;
        this.declinine = false;
        this.envelope = null;
    }
    async componentWillLoad() {
        var _a, _b, _c;
        try {
            // NOTE: The caller must have a signing session already active and shared to us via
            // the endpoint property.
            if (!this.envelopeId) {
                console.log(`[FOOTER] Missing required envelope ID ${this.envelopeId}`);
                return;
            }
            this.listenToEnvelope();
        }
        catch (e) {
            console.log('[FOOTER] 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('[FOOTER] Loading envelope', this.envelopeId);
        this.unlistenToEnvelope();
        Store.subscribe('envelopes', this.envelopeId, () => getEnvelope(this.endpoint, this.envelopeId), false, (envelope) => {
            this.envelope = envelope;
        });
    }
    unlistenToEnvelope() {
        if (this.envelopeListenerId) {
            Store.store.delListener(this.envelopeListenerId);
            this.envelopeListenerId = null;
        }
    }
    // handleRecipientAction(recipient: IRecipient, id: string) {
    //   console.log('[SIDEBAR] Recipient action', id, recipient);
    //   switch (id) {
    //     case 'update':
    //       this.showUpdateDialog = recipient.role_name;
    //       break;
    //
    //     case 'reminder':
    //       remindRecipient(this.endpoint, this.envelopeId, recipient.role_name)
    //         .then(() => {
    //           VerdocsToast('Reminder Sent', {style: 'success'});
    //         })
    //         .catch(e => {
    //           console.log('[SIDEBAR] Error resending invitation', e);
    //           VerdocsToast('Error resending invitation: ' + e.message, {style: 'error'});
    //         });
    //       break;
    //
    //     case 'reinvite':
    //       this.showReinviteDialog = recipient.role_name;
    //       break;
    //
    //     case 'inperson':
    //       this.showRecipientDialog = recipient.role_name;
    //       break;
    //
    //     case 'modify':
    //       VerdocsToast('This feature will be enabled in a future release. Please try again later.', {style: 'info'});
    //       break;
    //
    //     case 'details':
    //       VerdocsToast('This feature will be enabled in a future release. Please try again later.', {style: 'info'});
    //       break;
    //   }
    // }
    // handleUpdateRecipient(originalRecipient: IRecipient, updatedRecipient: IRecipient) {
    //   console.log('Updating recipient', originalRecipient, updatedRecipient);
    //   const fields: any = {};
    //   if (updatedRecipient.email !== originalRecipient.email) {
    //     fields.email = updatedRecipient.email;
    //   }
    //   if (updatedRecipient.phone !== originalRecipient.phone) {
    //     fields.phone = updatedRecipient.phone;
    //   }
    //   if (updatedRecipient.message !== originalRecipient.message) {
    //     fields.message = updatedRecipient.message;
    //   }
    //   if (updatedRecipient.first_name !== originalRecipient.first_name) {
    //     fields.first_name = updatedRecipient.first_name;
    //   }
    //   if (updatedRecipient.last_name !== originalRecipient.last_name) {
    //     fields.last_name = updatedRecipient.last_name;
    //   }
    //
    //   if (Object.keys(fields).length > 0) {
    //     updateRecipient(this.endpoint, this.envelopeId, originalRecipient.role_name, fields)
    //       .then(r => {
    //         // TODO: Reload the envelope?
    //         VerdocsToast('Recipient updated', {style: 'success'});
    //         console.log('[SIDEBAR] Updated recipient', r);
    //         Store.getEnvelope(this.endpoint, this.envelopeId, true);
    //         this.showUpdateDialog = '';
    //       })
    //       .catch(e => {
    //         VerdocsToast(e.response.data.error, {style: 'error'});
    //         this.showUpdateDialog = '';
    //       });
    //   } else {
    //     this.showUpdateDialog = '';
    //   }
    // }
    render() {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
        const hasPoweredBy = !!((_b = (_a = this.envelope) === null || _a === void 0 ? void 0 : _a.organization) === null || _b === void 0 ? void 0 : _b.powered_by_label);
        const hasLinks = !!((_d = (_c = this.envelope) === null || _c === void 0 ? void 0 : _c.organization) === null || _d === void 0 ? void 0 : _d.terms_use_url) || !!((_f = (_e = this.envelope) === null || _e === void 0 ? void 0 : _e.organization) === null || _f === void 0 ? void 0 : _f.privacy_policy_url);
        const hasButtons = !this.isDone;
        return (h(Host, { key: '1411feb1a6b519746b1bd2c6a5fce91715dd5485', class: { 'has-buttons': hasButtons, 'no-buttons': !hasButtons, 'has-powered-by': hasPoweredBy, 'has-links': hasLinks, 'just-buttons': !hasPoweredBy && !hasLinks } }, ((_h = (_g = this.envelope) === null || _g === void 0 ? void 0 : _g.organization) === null || _h === void 0 ? void 0 : _h.powered_by_label) && (h("div", { key: 'c5fc60d172ee3c69c4204256577749c001d53bb1', class: "powered-by" }, ((_k = (_j = this.envelope) === null || _j === void 0 ? void 0 : _j.organization) === null || _k === void 0 ? void 0 : _k.powered_by_label) ? (h("a", { href: (_m = (_l = this.envelope) === null || _l === void 0 ? void 0 : _l.organization) === null || _m === void 0 ? void 0 : _m.powered_by_url, target: "_blank", rel: "noopener noreferrer" }, h("span", null, (_p = (_o = this.envelope) === null || _o === void 0 ? void 0 : _o.organization) === null || _p === void 0 ? void 0 : _p.powered_by_label))) : (h("span", null, (_r = (_q = this.envelope) === null || _q === void 0 ? void 0 : _q.organization) === null || _r === void 0 ? void 0 : _r.powered_by_label)))), !this.isDone && (h("div", { key: '217521e7447870441d8565240750627b8133011d', class: "buttons" }, h("button", { key: '6d56085ff9ef8666a88c9966207dd28fb6caa5f6', onClick: () => (this.askingQuestion = true) }, h("span", { key: '36309d88efd6b195f5d4d2463c60bcc93bf1a600', innerHTML: QuestionIcon }), "Ask Sender a Question"), h("button", { key: '7d3a4362e849a77589625791b97ddf232cbdeb82', onClick: () => { var _a; return (_a = this.decline) === null || _a === void 0 ? void 0 : _a.emit(); } }, h("span", { key: '0c1577a95b4f1c0b4fa3860496433cc45af82c0b', innerHTML: DeclineIcon }), "Decline Signing"), h("button", { key: '9045872eaefa554da23170c02508c99c64a9966f', onClick: () => { var _a; return (_a = this.finishLater) === null || _a === void 0 ? void 0 : _a.emit(); } }, h("span", { key: '3ce2b170bffc99ef79c7931512f5a92cfff589f2', innerHTML: SaveIcon }), "Finish Later"))), h("div", { key: '198e4131f5e551021d54e8301e5142323ee765e7', class: "links" }, ((_t = (_s = this.envelope) === null || _s === void 0 ? void 0 : _s.organization) === null || _t === void 0 ? void 0 : _t.terms_use_url) && (h("a", { key: '9b5874f78e216330cde23dc5b62193dfa39a3722', href: (_v = (_u = this.envelope) === null || _u === void 0 ? void 0 : _u.organization) === null || _v === void 0 ? void 0 : _v.terms_use_url, target: "_blank", rel: "noopener noreferrer" }, h("span", { key: '657db0ecdabda0b4b83efc5424864a4ea422539c' }, "Terms of Use"))), ((_x = (_w = this.envelope) === null || _w === void 0 ? void 0 : _w.organization) === null || _x === void 0 ? void 0 : _x.privacy_policy_url) && (h("a", { key: 'd6dac069ba40c0691b6e7c38e52b928a2296eb0d', href: (_z = (_y = this.envelope) === null || _y === void 0 ? void 0 : _y.organization) === null || _z === void 0 ? void 0 : _z.privacy_policy_url, target: "_blank", rel: "noopener noreferrer" }, h("span", { key: '04a8ca34da1063fd368cfbf28a60673c69b02d3f' }, "Privacy Policy")))), this.askingQuestion && (h("verdocs-question-dialog", { key: 'acb26d8989d769e5b80f6bda2b0717822e53fc19', onNext: e => {
                var _a;
                (_a = this.askQuestion) === null || _a === void 0 ? void 0 : _a.emit({ question: e.detail.question });
                this.askingQuestion = false;
            }, onExit: () => (this.askingQuestion = false) }))));
    }
    static get is() { return "verdocs-sign-footer"; }
    static get originalStyleUrls() {
        return {
            "$": ["verdocs-sign-footer.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["verdocs-sign-footer.css"]
        };
    }
    static get properties() {
        return {
            "endpoint": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "VerdocsEndpoint",
                    "resolved": "VerdocsEndpoint",
                    "references": {
                        "VerdocsEndpoint": {
                            "location": "import",
                            "path": "@verdocs/js-sdk",
                            "id": "node_modules::VerdocsEndpoint"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The endpoint to use to communicate with Verdocs. If not set, the default endpoint will be used."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "VerdocsEndpoint.getDefault()"
            },
            "envelopeId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The envelope ID to render. Set ONE OF templateId or envelopeId. If both are set, envelopeId will be ignored."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "envelope-id",
                "defaultValue": "''"
            },
            "isDone": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If the recipient is \"done,\" some buttons will be hidden."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "is-done",
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "askingQuestion": {},
            "declinine": {},
            "envelope": {}
        };
    }
    static get events() {
        return [{
                "method": "askQuestion",
                "name": "askQuestion",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired if the user asks the sender a question. The parent component is responsible for handling this."
                },
                "complexType": {
                    "original": "{question: string}",
                    "resolved": "{ question: string; }",
                    "references": {}
                }
            }, {
                "method": "decline",
                "name": "decline",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired if the user asks the sender a question. The parent component is responsible for handling this."
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "finishLater",
                "name": "finishLater",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired if the user asks the sender a question. The parent component is responsible for handling this."
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "sdkError",
                "name": "sdkError",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired if an error occurs. The event details will contain information about the error. Most errors will\nterminate the process, and the calling application should correct the condition and re-render the component."
                },
                "complexType": {
                    "original": "SDKError",
                    "resolved": "SDKError",
                    "references": {
                        "SDKError": {
                            "location": "import",
                            "path": "../../../utils/errors",
                            "id": "src/utils/errors.ts::SDKError"
                        }
                    }
                }
            }];
    }
}
//# sourceMappingURL=verdocs-sign-footer.js.map
