import { assertExists, Slot } from '@blockeditor/global/utils';
import { nanoid } from '@blockeditor/store';
import { getTagColor, selectOptionColors, } from '../../../_common/components/tags/colors.js';
import { selectPureColumnConfig } from '../columns/select/define.js';
import { textPureColumnConfig } from '../columns/text/define.js';
import { BaseDataSource } from './base.js';
export class TagsDatasource extends BaseDataSource {
    rowMove(rowId, position) {
        // not support
        rowId;
        position;
    }
    get rows() {
        return this.tags.map(v => v.id);
    }
    get properties() {
        return Object.keys(this.propertiesMap);
    }
    constructor(host, _config) {
        super();
        this.propertiesMap = {
            value: {
                type: textPureColumnConfig.type,
                getValue: tag => tag.value,
                setValue: (tag, value) => {
                    this.changeTag({
                        ...tag,
                        value: `${value ?? ''}`,
                    });
                },
            },
            color: {
                type: selectPureColumnConfig.type,
                getValue: tag => tag.color,
                setValue: (tag, value) => {
                    this.changeTag({
                        ...tag,
                        color: `${value ?? ''}`,
                    });
                },
                getData: () => ({
                    options: selectOptionColors.map(v => ({
                        id: v.color,
                        value: v.name,
                        color: v.color,
                    })),
                }),
            },
            parent: {
                type: selectPureColumnConfig.type,
                getData: () => ({ options: this.meta.properties.tags?.options ?? [] }),
                getValue: tag => tag.parentId,
                setValue: (tag, value) => {
                    this.changeTag({
                        ...tag,
                        parentId: `${value ?? ''}`,
                    });
                },
            },
        };
        this.slots = {
            update: new Slot(),
        };
        this.changeTags = (tags) => {
            this.meta.setProperties({
                ...this.meta.properties,
                tags: { options: tags },
            });
        };
        this.changeTag = (tag) => {
            this.changeTags(this.tags.map(v => (v.id === tag.id ? tag : v)));
        };
        this.allPropertyConfig = [];
        this.meta = host.page.workspace.meta;
        host.page.workspace.meta.pageMetasUpdated.pipe(this.slots.update);
    }
    cellChangeValue(rowId, propertyId, value) {
        const tag = this.tags.find(v => v.id === rowId);
        if (tag) {
            this.propertiesMap[propertyId]?.setValue?.(tag, value);
        }
    }
    cellGetRenderValue(rowId, propertyId) {
        return this.cellGetValue(rowId, propertyId);
    }
    cellGetValue(rowId, propertyId) {
        const tag = this.tags.find(v => v.id === rowId);
        assertExists(tag);
        return this.propertiesMap[propertyId]?.getValue(tag);
    }
    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) {
        const id = nanoid('unknown');
        this.changeTags([
            ...this.tags,
            {
                id,
                value: '',
                color: getTagColor(),
            },
        ]);
        return id;
    }
    rowDelete(ids) {
        this.changeTags(this.tags.filter(v => !ids.includes(v.id)));
    }
    get tags() {
        return this.meta.properties.tags?.options ?? [];
    }
}
//# sourceMappingURL=tags-datasource.js.map