import { BlockModel, defineBlockSchema } from '@blockeditor/store';
export class DataViewBlockModel extends BlockModel {
    constructor() {
        super();
        this.created.on(() => {
            if (!this.views.length) {
                this.addView('table');
            }
        });
    }
    addView(mode) {
        this.page.captureSync();
        const id = this.page.generateBlockId();
        this.page.transact(() => {
            this.views.push({
                id,
                mode,
                columns: [],
                name: mode,
                filter: { type: 'group', op: 'and', conditions: [] },
            });
        });
        return id;
    }
    deleteView(id) {
        this.page.captureSync();
        this.page.transact(() => {
            this.views = this.views.filter(v => v.id !== id);
        });
    }
    updateView(id, update) {
        this.page.transact(() => {
            this.views = this.views.map(v => {
                if (v.id !== id) {
                    return v;
                }
                return { ...v, ...update(v) };
            });
        });
        this.applyViewsUpdate();
    }
    applyViewsUpdate() {
        this.page.updateBlock(this, {
            views: this.views,
        });
    }
}
export const DataViewBlockSchema = defineBlockSchema({
    flavour: 'affine:data-view',
    props: () => ({
        views: [],
    }),
    metadata: {
        role: 'hub',
        version: 1,
        parent: ['affine:note'],
        children: ['affine:paragraph', 'affine:list'],
    },
    toModel: () => {
        return new DataViewBlockModel();
    },
});
//# sourceMappingURL=data-view-model.js.map