import { BlockService } from '@blockeditor/block-std';
import { assertExists } from '@blockeditor/global/utils';
import { FileDropManager, } from '../_common/components/file-drop-manager.js';
import { setImageProxyMiddlewareURL } from '../_common/transformers/middlewares.js';
import { isInsideEdgelessEditor, matchFlavours, } from '../_common/utils/index.js';
import { ImageSelection } from './image-selection.js';
import { addSiblingImageBlock } from './utils.js';
export class ImageService extends BlockService {
    constructor() {
        super(...arguments);
        this.maxFileSize = 10 * 1000 * 1000; // 10MB (default)
        this._fileDropOptions = {
            flavour: this.flavour,
            onDrop: async ({ files, targetModel, place, point }) => {
                const imageFiles = files.filter(file => file.type.startsWith('image/'));
                if (targetModel && !matchFlavours(targetModel, ['affine:surface'])) {
                    addSiblingImageBlock(imageFiles, this.maxFileSize, targetModel, place);
                }
                else if (isInsideEdgelessEditor(this.std.host)) {
                    const edgelessPage = this
                        .pageBlockComponent;
                    await edgelessPage.addImages(imageFiles, point);
                }
                return true;
            },
        };
        this.setImageProxyMiddlewareURL = setImageProxyMiddlewareURL;
    }
    get pageBlockComponent() {
        const pageBlock = this.page.root;
        assertExists(pageBlock);
        const pageElement = this.std.view.viewFromPath('block', [pageBlock.id]);
        assertExists(pageElement);
        return pageElement;
    }
    mounted() {
        super.mounted();
        this.selectionManager.register(ImageSelection);
        this.fileDropManager = new FileDropManager(this, this._fileDropOptions);
    }
}
//# sourceMappingURL=image-service.js.map