import { h } from "@stencil/core";
import { DownloadAction } from "../../../utils/Types";
const DocumentIcon = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M13 2H6C5.46957 2 4.96086 2.21071 4.58579 2.58579C4.21071 2.96086 4 3.46957 4 4V20C4 20.5304 4.21071 21.0391 4.58579 21.4142C4.96086 21.7893 5.46957 22 6 22H18C18.5304 22 19.0391 21.7893 19.4142 21.4142C19.7893 21.0391 20 20.5304 20 20V9L13 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M13 2V9H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
const CertificateIcon = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 15C15.866 15 19 11.866 19 8C19 4.13401 15.866 1 12 1C8.13401 1 5 4.13401 5 8C5 11.866 8.13401 15 12 15Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M8.21 13.89L7 23L12 20L17 23L15.79 13.88" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
const ZipIcon = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M10 16V22H14V16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M6 16H18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M20 22H4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M12 10L12 16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M8 12L12 16L16 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M4 16V4H20V16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
const CheckIcon = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M20 6L9 17L4 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
const RefreshIcon = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M23 4V10H17" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M20.49 15A9 9 0 1 1 21.23 8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
export class VerdocsDownloadDialog {
    constructor() {
        /**
         * If true, the envelope is considered signed.
         */
        this.signed = false;
        /**
         * If true, we are currently polling the server for updates.
         */
        this.polling = false;
        /**
         * The list of documents in the envelope.
         */
        this.documents = [];
        /**
         * If true, the envelope has a certificate available for download.
         */
        this.hasCertificate = false;
    }
    handleOptionClick(action, documentId) {
        const hasCert = this.documents.some(d => d.type === 'certificate') || this.hasCertificate;
        const isCertReady = this.signed && hasCert;
        const isCertificateDisabled = action === DownloadAction.certificate && !isCertReady;
        const isZipDisabled = action === DownloadAction.zip && (this.polling || !isCertReady);
        const isCombinedDisabled = action === DownloadAction.combined && !isCertReady;
        if (isCertificateDisabled || isZipDisabled || isCombinedDisabled)
            return;
        this.download.emit({ action, documentId });
    }
    render() {
        const attachments = this.documents.filter(d => d.type === 'attachment').sort((a, b) => (a.order !== b.order ? a.order - b.order : a.created_at.localeCompare(b.created_at)));
        const hasCertificateDoc = this.documents.some(d => d.type === 'certificate') || this.hasCertificate;
        const certReady = this.signed && hasCertificateDoc;
        const isCombinedReady = this.signed && hasCertificateDoc;
        const allDone = !this.polling && certReady;
        const attachmentBusy = !this.signed;
        return (h("verdocs-dialog", { key: 'a877948153b6b0ee7eda1f2e19d4df7f061f72d5', onExit: () => this.exit.emit() }, h("div", { key: '51da52f3f20473ae1901ce60e5edafd144521478', slot: "heading", class: "heading-container" }, h("h3", { key: 'a68c34c0de3959409e1939aed35879402cba2b09', style: { margin: '0', fontSize: '1.25rem', fontWeight: '600', padding: '16px 24px' } }, "Download")), h("div", { key: '66ccef7e477d5862f8d941ec558310aaffd9e266', slot: "content", class: "content" }, attachments.length <= 2 &&
            attachments.map(doc => (h("div", { class: "download-option", onClick: () => this.handleOptionClick(DownloadAction.document, doc.id) }, h("div", { class: "icon-container", innerHTML: DocumentIcon }), h("div", { class: "text-container" }, h("div", { class: "label" }, doc.name), h("div", { class: "description" }, "Download the document")), attachmentBusy ? (h("div", { class: "status-indicator" }, h("div", { class: "spinner-inline", innerHTML: RefreshIcon }))) : (h("div", { class: "status-indicator" }, h("div", { class: "signed", innerHTML: CheckIcon }), h("span", null, "Signed")))))), attachments.length > 2 && h("div", { key: '6eda124edbc14116b63bb1a508d5f6ca2c4ee81d', class: "info-message" }, "Multiple documents attached. Please use the ZIP option below to download all files."), h("div", { key: 'c7adc5310b532cb7ca3ce64d90547c0648675af9', class: { 'download-option': true, 'disabled': !certReady }, onClick: () => this.handleOptionClick(DownloadAction.certificate), title: !certReady ? 'Certificate not yet available' : '' }, h("div", { key: '099cece3ea7a1927a8b7d9632ffa8cc3e6220868', class: "icon-container", innerHTML: CertificateIcon }), h("div", { key: '0e09d9749a9967291fdde0a35a30287d31bedec7', class: "text-container" }, h("div", { key: '3f76cc900207bf06c5207d79d68b1219c066bde8', class: "label" }, "Certificate"), h("div", { key: '34fe2b37c150b66173f61f76cc5d8d77c74e61e8', class: "description" }, "Download the certificate")), certReady ? (h("div", { class: "status-indicator" }, h("div", { class: "signed", innerHTML: CheckIcon }), h("span", null, "Ready"))) : (h("div", { class: "status-indicator" }, h("div", { class: "spinner-inline", innerHTML: RefreshIcon })))), h("div", { key: '2e1d48989564753b19f7431db7b231f1858542ce', class: { 'download-option': true, 'disabled': !isCombinedReady }, onClick: () => this.handleOptionClick(DownloadAction.combined), title: !isCombinedReady ? 'Waiting for all documents to be ready' : '' }, h("div", { key: 'de52875477a5f583624357061ee21ab65fbbf53a', class: "icon-container", innerHTML: ZipIcon }), h("div", { key: 'c7ce9b867f7e6f5eec060a65d4487e3f33814025', class: "text-container" }, h("div", { key: '63ffd99d31428740c9b19ea44281a4b966c4dab8', class: "label" }, "Combined"), h("div", { key: '5ff20fcacc17c4dbee9034181158a5202a3391b5', class: "description" }, "Merge envelopes & certificate into a single PDF")), isCombinedReady ? (h("div", { class: "status-indicator" }, h("div", { class: "signed", innerHTML: CheckIcon }), h("span", null, "Ready"))) : (h("div", { class: "status-indicator" }, h("div", { class: "spinner-inline", innerHTML: RefreshIcon })))), h("div", { key: 'feb5a60d1538f3367b082f5c8f2b41fde4c44997', class: { 'download-option': true, 'disabled': !allDone }, onClick: () => this.handleOptionClick(DownloadAction.zip), title: !allDone ? 'Waiting for all documents to be ready' : '' }, h("div", { key: 'dcad103a22173198f247005ba7cd611986865f89', class: "icon-container", innerHTML: ZipIcon }), h("div", { key: 'eccfb0e7f4a7aca6b180089a0b2392ec51e8a21d', class: "text-container" }, h("div", { key: '94586ca4646cab77493d98e85e3f0a1bcf7cc911', class: "label" }, "All Files"), h("div", { key: '2d42fa14e73f9df2e39fcd8ff8f9cb107ffd2246', class: "description" }, "Download everything as a ZIP file")), allDone ? (h("div", { class: "status-indicator" }, h("div", { class: "signed", innerHTML: CheckIcon }), h("span", null, "Ready"))) : (h("div", { class: "status-indicator" }, h("div", { class: "spinner-inline", innerHTML: RefreshIcon }))))), h("div", { key: 'a087605743e0b7e7f3de01aa20dc5be28b8723d5', slot: "footer", style: { display: 'none' } })));
    }
    static get is() { return "verdocs-download-dialog"; }
    static get originalStyleUrls() {
        return {
            "$": ["verdocs-download-dialog.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["verdocs-download-dialog.css"]
        };
    }
    static get properties() {
        return {
            "signed": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If true, the envelope is considered signed."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "signed",
                "defaultValue": "false"
            },
            "polling": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If true, we are currently polling the server for updates."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "polling",
                "defaultValue": "false"
            },
            "documents": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "any[]",
                    "resolved": "any[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The list of documents in the envelope."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "[]"
            },
            "hasCertificate": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "If true, the envelope has a certificate available for download."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "has-certificate",
                "defaultValue": "false"
            }
        };
    }
    static get events() {
        return [{
                "method": "download",
                "name": "download",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired when an option is selected."
                },
                "complexType": {
                    "original": "{action: DownloadAction; documentId?: string}",
                    "resolved": "{ action: DownloadAction; documentId?: string; }",
                    "references": {
                        "DownloadAction": {
                            "location": "import",
                            "path": "../../../utils/Types",
                            "id": "src/utils/Types.ts::DownloadAction",
                            "referenceLocation": "DownloadAction"
                        }
                    }
                }
            }, {
                "method": "exit",
                "name": "exit",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Event fired when Cancel is pressed or background is clicked."
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }];
    }
}
//# sourceMappingURL=verdocs-download-dialog.js.map
