import { p as proxyCustomElement, H, c as createEvent, h, d as Host, F as Fragment } from './p-D16h8pzh.js';
import { VerdocsEndpoint, getTemplate, integerSequence } from '@verdocs/js-sdk';
import { S as SDKError } from './p-C1sRVZWv.js';
import { S as Store } from './p-DmsV595e.js';
import { d as defineCustomElement$p } from './p-BNrp2UP2.js';
import { d as defineCustomElement$o } from './p-wYGYhfrp.js';
import { d as defineCustomElement$n } from './p-DsCKNQPC.js';
import { d as defineCustomElement$m } from './p-CZTe2XIO.js';
import { d as defineCustomElement$l } from './p-CTzrE2_W.js';
import { d as defineCustomElement$k } from './p-MX0H_yx6.js';
import { d as defineCustomElement$j } from './p-CRR4fTl9.js';
import { d as defineCustomElement$i } from './p-Clt27_Qj.js';
import { d as defineCustomElement$h } from './p-CsyhCKk4.js';
import { d as defineCustomElement$g } from './p-xIC5Vhhx.js';
import { d as defineCustomElement$f } from './p-Sk_71M_-.js';
import { d as defineCustomElement$e } from './p-BeSHNILk.js';
import { d as defineCustomElement$d } from './p-D-GZw56Q.js';
import { d as defineCustomElement$c } from './p-DF8La-N2.js';
import { d as defineCustomElement$b } from './p-BGZ_owBm.js';
import { d as defineCustomElement$a } from './p-kCaWRqn0.js';
import { d as defineCustomElement$9 } from './p-Bv-L0hsU.js';
import { d as defineCustomElement$8 } from './p-CQ-UXlBO.js';
import { d as defineCustomElement$7 } from './p-Boia1Nbz.js';
import { d as defineCustomElement$6 } from './p-Cy-AXBKH.js';
import { d as defineCustomElement$5 } from './p-DoO2XAMG.js';
import { d as defineCustomElement$4 } from './p-CNvRd0Kp.js';
import { d as defineCustomElement$3 } from './p-vCuIwe1D.js';
import { d as defineCustomElement$2 } from './p-C_SISdpZ.js';
import { d as defineCustomElement$1 } from './p-CcglHGeY.js';

const verdocsPreviewCss = "@-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-preview{font-family:\"Inter\", -apple-system, \"Segoe UI\", \"Roboto\", \"Helvetica Neue\", 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() {
        super();
        this.__registerHost();
        this.sdkError = createEvent(this, "sdkError");
        this.templateListenerId = null;
        /**
         * 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) {
        // const pageInfo = e.detail as IDocumentPageInfo;
        // const fields = (this.template?.fields || []).filter(field => field.page === pageInfo.pageNumber);
        // console.log('[PREVIEW] Page rendered', pageInfo, fields);
        // fields.forEach(field => renderDocumentField(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-help-icon", "verdocs-initial-dialog", "verdocs-loader", "verdocs-ok-dialog", "verdocs-portal", "verdocs-select-input", "verdocs-signature-dialog", "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$p();
            }
            break;
        case "verdocs-checkbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$o();
            }
            break;
        case "verdocs-component-error":
            if (!customElements.get(tagName)) {
                defineCustomElement$n();
            }
            break;
        case "verdocs-dialog":
            if (!customElements.get(tagName)) {
                defineCustomElement$m();
            }
            break;
        case "verdocs-field-attachment":
            if (!customElements.get(tagName)) {
                defineCustomElement$l();
            }
            break;
        case "verdocs-field-checkbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$k();
            }
            break;
        case "verdocs-field-date":
            if (!customElements.get(tagName)) {
                defineCustomElement$j();
            }
            break;
        case "verdocs-field-dropdown":
            if (!customElements.get(tagName)) {
                defineCustomElement$i();
            }
            break;
        case "verdocs-field-initial":
            if (!customElements.get(tagName)) {
                defineCustomElement$h();
            }
            break;
        case "verdocs-field-radio":
            if (!customElements.get(tagName)) {
                defineCustomElement$g();
            }
            break;
        case "verdocs-field-signature":
            if (!customElements.get(tagName)) {
                defineCustomElement$f();
            }
            break;
        case "verdocs-field-textarea":
            if (!customElements.get(tagName)) {
                defineCustomElement$e();
            }
            break;
        case "verdocs-field-textbox":
            if (!customElements.get(tagName)) {
                defineCustomElement$d();
            }
            break;
        case "verdocs-field-timestamp":
            if (!customElements.get(tagName)) {
                defineCustomElement$c();
            }
            break;
        case "verdocs-help-icon":
            if (!customElements.get(tagName)) {
                defineCustomElement$b();
            }
            break;
        case "verdocs-initial-dialog":
            if (!customElements.get(tagName)) {
                defineCustomElement$a();
            }
            break;
        case "verdocs-loader":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "verdocs-ok-dialog":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "verdocs-portal":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "verdocs-select-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "verdocs-signature-dialog":
            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-BHeendgg.js.map

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