import { p as proxyCustomElement, H, c as createEvent, h, d as Host, F as Fragment } from './p-CUAGH_8P.js';
import { VerdocsEndpoint, getTemplate, integerSequence } from '@verdocs/js-sdk';
import { a as renderDocumentField } from './p-Dk8BEvRP.js';
import { S as SDKError } from './p-C1sRVZWv.js';
import { S as Store } from './p-bS8INhRF.js';
import { d as defineCustomElement$o } from './p-C_yyK5xE.js';
import { d as defineCustomElement$n } from './p-BvV9rOTP.js';
import { d as defineCustomElement$m } from './p-CdmWI0-q.js';
import { d as defineCustomElement$l } from './p-Ba_mXs1J.js';
import { d as defineCustomElement$k } from './p-BFALqJGq.js';
import { d as defineCustomElement$j } from './p-BnrYh7lb.js';
import { d as defineCustomElement$i } from './p-CC0KCzJ2.js';
import { d as defineCustomElement$h } from './p-CbqdqC8j.js';
import { d as defineCustomElement$g } from './p-1yLYyKxs.js';
import { d as defineCustomElement$f } from './p-CdaVcvEc.js';
import { d as defineCustomElement$e } from './p-DzbNugSF.js';
import { d as defineCustomElement$d } from './p-CStsF8-Q.js';
import { d as defineCustomElement$c } from './p-Bpu-zxZH.js';
import { d as defineCustomElement$b } from './p-jeLHHnry.js';
import { d as defineCustomElement$a } from './p-BC5N3FIA.js';
import { d as defineCustomElement$9 } from './p-CnPaJH5T.js';
import { d as defineCustomElement$8 } from './p-CNKACBaL.js';
import { d as defineCustomElement$7 } from './p-o9BQIoXw.js';
import { d as defineCustomElement$6 } from './p-DUYRis3A.js';
import { d as defineCustomElement$5 } from './p-C1AtapGD.js';
import { d as defineCustomElement$4 } from './p-D2FCvVcG.js';
import { d as defineCustomElement$3 } from './p-ClKxADje.js';
import { d as defineCustomElement$2 } from './p-BhfPQRmb.js';
import { d as defineCustomElement$1 } from './p-DmEw4Bbl.js';

const verdocsPreviewCss = "verdocs-preview{font-family:var(--verdocs-primary-font, \"Inter\", \"Barlow\", sans-serif);display:-ms-flexbox;display:flex;padding:15px;row-gap:15px;min-height:200px;position:relative;-ms-flex-align:center;align-items:center;-webkit-box-sizing:border-box;box-sizing:border-box;-ms-flex-direction:column;flex-direction:column}verdocs-preview div,verdocs-preview canvas{-webkit-box-sizing:border-box;box-sizing:border-box}";

const VerdocsPreview = /*@__PURE__*/ proxyCustomElement(class VerdocsPreview extends H {
    constructor(registerHost) {
        super();
        if (registerHost !== false) {
            this.__registerHost();
        }
        this.sdkError = createEvent(this, "sdkError");
        this.templateListenerId = null;
        this.renderedPages = {};
        /**
         * The endpoint to use to communicate with Verdocs. If not set, the default endpoint will be used.
         */
        this.endpoint = VerdocsEndpoint.getDefault();
        /**
         * The ID of the template to create the document from.
         */
        this.templateId = null;
        this.loading = true;
        this.template = null;
    }
    disconnectedCallback() {
        this.unlistenToTemplate();
    }
    async listenToTemplate() {
        this.unlistenToTemplate();
        Store.subscribe('templates', this.templateId, () => getTemplate(this.endpoint, this.templateId), false, (template) => {
            this.template = template;
            this.loading = false;
        });
    }
    unlistenToTemplate() {
        if (this.templateListenerId) {
            Store.store.delListener(this.templateListenerId);
            this.templateListenerId = null;
        }
    }
    onTemplateIdChanged(newTemplateId) {
        console.log('[PREVIEW] Template ID changed', newTemplateId);
        this.listenToTemplate();
    }
    async componentWillLoad() {
        var _a, _b, _c;
        try {
            this.endpoint.loadSession();
            if (!this.endpoint.session) {
                console.log('[PREVIEW] Unable to start builder session, must be authenticated');
                return;
            }
            if (!this.templateId) {
                console.log(`[PREVIEW] Missing required template ID ${this.templateId}`);
                return;
            }
            this.listenToTemplate();
        }
        catch (e) {
            console.log('[PREVIEW] Error with preview session', 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));
        }
    }
    handlePageRendered(e) {
        var _a;
        const pageInfo = e.detail;
        this.renderedPages[`${pageInfo.documentId}:${pageInfo.pageNumber}`] = pageInfo;
        const fields = (((_a = this.template) === null || _a === void 0 ? void 0 : _a.fields) || []).filter(field => {
            // Templates created on the new builder have document_id set, but old ones may not.
            // If document_id is missing, we assume it's for the first document or use legacy logic.
            // However, for this fix, we primarily care about multi-doc templates which should have IDs.
            if (field['document_id']) {
                return field['document_id'] === pageInfo.documentId && field.page === pageInfo.pageNumber;
            }
            return field.page === pageInfo.pageNumber;
        });
        // console.log('[PREVIEW] Page rendered', pageInfo, fields);
        fields.forEach(field => renderDocumentField('template', field, pageInfo, { disabled: true, editable: false, draggable: false }));
    }
    render() {
        var _a;
        if (this.loading) {
            return (h(Host, null, h("verdocs-loader", null)));
        }
        return (h(Host, null, (((_a = this.template) === null || _a === void 0 ? void 0 : _a.documents) || []).map(document => {
            const pageNumbers = integerSequence(1, document.pages);
            return (h(Fragment, null, pageNumbers.map(pageNumber => {
                var _a;
                const pageSize = ((_a = document.page_sizes) === null || _a === void 0 ? void 0 : _a[pageNumber]) || { width: 612, height: 792 };
                return (h("verdocs-template-document-page", { templateId: document.template_id, documentId: document.id, pageNumber: pageNumber, disabled: true, editable: false, done: false, virtualWidth: pageSize.width, virtualHeight: pageSize.height, onPageRendered: e => this.handlePageRendered(e), layers: [
                        { name: 'page', type: 'canvas' },
                        { name: 'controls', type: 'div' },
                    ] }));
            })));
        })));
    }
    static get watchers() { return {
        "templateId": ["onTemplateIdChanged"]
    }; }
    static get style() { return verdocsPreviewCss; }
}, [256, "verdocs-preview", {
        "endpoint": [16],
        "templateId": [1, "template-id"],
        "loading": [32],
        "template": [32]
    }, undefined, {
        "templateId": ["onTemplateIdChanged"]
    }]);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["verdocs-preview", "verdocs-button", "verdocs-checkbox", "verdocs-component-error", "verdocs-dialog", "verdocs-field-attachment", "verdocs-field-checkbox", "verdocs-field-date", "verdocs-field-dropdown", "verdocs-field-initial", "verdocs-field-radio", "verdocs-field-signature", "verdocs-field-textarea", "verdocs-field-textbox", "verdocs-field-timestamp", "verdocs-flag", "verdocs-help-icon", "verdocs-loader", "verdocs-ok-dialog", "verdocs-portal", "verdocs-select-input", "verdocs-template-document-page", "verdocs-template-field-properties", "verdocs-text-input", "verdocs-upload-dialog"];
    components.forEach(tagName => { switch (tagName) {
        case "verdocs-preview":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, VerdocsPreview);
            }
            break;
        case "verdocs-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$o();
            }
            break;
        case "verdocs-checkbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$n();
            }
            break;
        case "verdocs-component-error":
            if (!customElements.get(tagName)) {
                defineCustomElement$m();
            }
            break;
        case "verdocs-dialog":
            if (!customElements.get(tagName)) {
                defineCustomElement$l();
            }
            break;
        case "verdocs-field-attachment":
            if (!customElements.get(tagName)) {
                defineCustomElement$k();
            }
            break;
        case "verdocs-field-checkbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$j();
            }
            break;
        case "verdocs-field-date":
            if (!customElements.get(tagName)) {
                defineCustomElement$i();
            }
            break;
        case "verdocs-field-dropdown":
            if (!customElements.get(tagName)) {
                defineCustomElement$h();
            }
            break;
        case "verdocs-field-initial":
            if (!customElements.get(tagName)) {
                defineCustomElement$g();
            }
            break;
        case "verdocs-field-radio":
            if (!customElements.get(tagName)) {
                defineCustomElement$f();
            }
            break;
        case "verdocs-field-signature":
            if (!customElements.get(tagName)) {
                defineCustomElement$e();
            }
            break;
        case "verdocs-field-textarea":
            if (!customElements.get(tagName)) {
                defineCustomElement$d();
            }
            break;
        case "verdocs-field-textbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$c();
            }
            break;
        case "verdocs-field-timestamp":
            if (!customElements.get(tagName)) {
                defineCustomElement$b();
            }
            break;
        case "verdocs-flag":
            if (!customElements.get(tagName)) {
                defineCustomElement$a();
            }
            break;
        case "verdocs-help-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "verdocs-loader":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "verdocs-ok-dialog":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "verdocs-portal":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "verdocs-select-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "verdocs-template-document-page":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "verdocs-template-field-properties":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "verdocs-text-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "verdocs-upload-dialog":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}

export { VerdocsPreview as V, defineCustomElement as d };
//# sourceMappingURL=p-XhWO_bzn.js.map

//# sourceMappingURL=p-XhWO_bzn.js.map