// FIXME: this should be move to more appropriate place after extension system has completed
import { assertExists } from '@blockeditor/global/utils';
import { GradioApp } from './gradio-app.js';
function createGradioApp() {
    const app = new GradioApp();
    return app;
}
function getPageElement(host) {
    assertExists(host.page.root?.id);
    const page = host.view.viewFromPath('block', [
        host.page.root.id,
    ]);
    return page;
}
export function openLeditsEditor(model, blob, host) {
    const pageElement = getPageElement(host);
    const app = createGradioApp();
    const modal = pageElement.widgetElements['affine-modal-widget'].open({
        footer: [
            {
                text: 'Cancel',
                onClick: () => {
                    modal.close();
                },
            },
            {
                type: 'primary',
                text: 'Save',
                onClick: async () => {
                    const newBlob = await app.exportImage();
                    if (!newBlob || newBlob === blob) {
                        modal.close();
                    }
                    const blobManager = model.page.blob;
                    blobManager
                        .set(newBlob)
                        .then(sourceId => {
                        model.page.updateBlock(model, {
                            sourceId,
                        });
                        modal.close();
                    })
                        .catch(console.error);
                },
            },
        ],
        entry(div) {
            app.imageBlob = blob;
            div.appendChild(app);
        },
    });
}
//# sourceMappingURL=main.js.map