import { assertExists, Slot } from '@blockeditor/global/utils';
import { multiSelectPureColumnConfig } from '../columns/multi-select/define.js';
import { numberPureColumnConfig } from '../columns/number/define.js';
import { textPureColumnConfig } from '../columns/text/define.js';
import { BaseDataSource } from './base.js';
export class AllPageDatasource extends BaseDataSource {
    get rows() {
        return Array.from(this.workspace.pages.keys());
    }
    get properties() {
        return Object.keys(this.propertiesMap);
    }
    constructor(host, _config) {
        super();
        this.propertiesMap = {
            title: {
                type: textPureColumnConfig.type,
                getValue: page => page.meta.title,
                setValue: (page, value) => {
                    page.meta.title = `${value ?? ''}`;
                },
            },
            tags: {
                type: multiSelectPureColumnConfig.type,
                getValue: page => page.meta.tags,
                setValue: (page, value) => {
                    page.meta.tags = value;
                },
                getData: () => ({
                    options: this.workspace.meta.properties.tags?.options ?? [],
                }),
                changeData: data => {
                    this.workspace.meta.setProperties({
                        ...this.workspace.meta.properties,
                        tags: data,
                    });
                },
            },
            createDate: {
                type: numberPureColumnConfig.type,
                getValue: page => page.meta.createDate,
            },
        };
        this.slots = {
            update: new Slot(),
        };
        this.allPropertyConfig = [];
        this.workspace = host.page.workspace;
        host.page.workspace.meta.pageMetasUpdated.pipe(this.slots.update);
    }
    cellChangeValue(rowId, propertyId, value) {
        const page = this.workspace.getPage(rowId);
        assertExists(page);
        this.propertiesMap[propertyId]?.setValue?.(page, value);
    }
    cellGetRenderValue(rowId, propertyId) {
        return this.cellGetValue(rowId, propertyId);
    }
    cellGetValue(rowId, propertyId) {
        const page = this.workspace.getPage(rowId);
        assertExists(page);
        return this.propertiesMap[propertyId]?.getValue(page);
    }
    propertyAdd(_insertPosition) {
        throw new Error('not support');
    }
    propertyChangeData(propertyId, data) {
        this.propertiesMap[propertyId]?.changeData?.(data);
    }
    propertyChangeName(_propertyId, _name) {
        // not support
    }
    propertyChangeType(_propertyId, _type) {
        // not support
    }
    propertyDelete(_id) {
        // not support
    }
    propertyDuplicate(_columnId) {
        throw new Error('not support');
    }
    propertyGetData(propertyId) {
        return this.propertiesMap[propertyId]?.getData?.() ?? {};
    }
    propertyGetName(propertyId) {
        return propertyId;
    }
    propertyGetType(propertyId) {
        return this.propertiesMap[propertyId].type;
    }
    rowAdd(_insertPosition) {
        return this.workspace.createPage().id;
    }
    rowDelete(ids) {
        ids.forEach(id => this.workspace.removePage(id));
    }
    rowMove(rowId, position) {
        // not support
        rowId;
        position;
    }
}
//# sourceMappingURL=all-page-datasource.js.map