var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { css, html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
import { createInlineIframe } from './utils.js';
let GradioApp = class GradioApp extends LitElement {
    constructor() {
        super(...arguments);
        this._loaded = false;
        this._setImage = (iframe) => {
            iframe.contentWindow?.postMessage({
                type: 'set_image',
                image: this.imageBlob,
            }, '*');
        };
    }
    firstUpdated() {
        const iframe = this.renderRoot.querySelector('iframe');
        if (!iframe)
            return;
        iframe.src = createInlineIframe();
        iframe.onload = () => {
            this._setImage(iframe);
            iframe.onload = null;
        };
        this._iframe = iframe;
        this._loaded = true;
    }
    exportImage() {
        if (!this._iframe || !this._loaded || !this._iframe.contentWindow)
            return Promise.resolve(this.imageBlob);
        const iframeContentWindow = this._iframe.contentWindow;
        const p = new Promise(resolve => {
            window.addEventListener('message', e => {
                const blob = e.data?.blob;
                resolve(blob);
            }, {
                once: true,
            });
            iframeContentWindow.postMessage({
                type: 'export_image',
            }, '*');
        });
        return p;
    }
    render() {
        return html `<div class="gradio-app-wrapper">
      <iframe
        style="border: 0;width: 100%; height: 100%"
        sandbox="allow-forms allow-scripts allow-downloads"
        src="about:blank"
      ></iframe>
    </div>`;
    }
};
GradioApp.styles = css `
    .gradio-app-wrapper {
      height: 100%;
      display: flex;
      flex-direction: column;
    }
    .gradio-app-footer {
      display: flex;
      justify-content: flex-end;
      gap: 2rem;
    }
  `;
GradioApp = __decorate([
    customElement('ledits-gradio-app')
], GradioApp);
export { GradioApp };
//# sourceMappingURL=gradio-app.js.map