import { assertExists } from '@blockeditor/global/utils';
import { BaseAdapter } from '@blockeditor/store';
import { decode, encode } from './utils.js';
export class ClipboardAdapter extends BaseAdapter {
    fromPageSnapshot(_payload) {
        throw new Error('not implemented');
    }
    toPageSnapshot(_payload) {
        throw new Error('not implemented');
    }
    fromBlockSnapshot(_payload) {
        throw new Error('not implemented');
    }
    toBlockSnapshot(_payload) {
        throw new Error('not implemented');
    }
    async fromSliceSnapshot(payload) {
        const snapshot = payload.snapshot;
        const assets = payload.assets;
        assertExists(assets);
        const map = assets.getAssets();
        const blobs = {};
        await Promise.all(Array.from(map.entries()).map(async ([id, blob]) => {
            const content = encode(await blob.arrayBuffer());
            const file = {
                name: blob.name,
                type: blob.type,
                content,
            };
            blobs[id] = file;
        }));
        return {
            file: JSON.stringify({
                snapshot,
                blobs,
            }),
            assetsIds: [],
        };
    }
    toSliceSnapshot(payload) {
        const json = JSON.parse(payload.file);
        const { blobs, snapshot } = json;
        const map = payload.assets?.getAssets();
        Object.entries(blobs).forEach(([sourceId, file]) => {
            const blob = new Blob([decode(file.content)]);
            const f = new File([blob], file.name, {
                type: file.type,
            });
            assertExists(map);
            map.set(sourceId, f);
        });
        return Promise.resolve(snapshot);
    }
}
ClipboardAdapter.MIME = 'BLOCKSUITE/SNAPSHOT';
//# sourceMappingURL=adapter.js.map