import path from 'path';
import DateStore from '@seald-io/nedb';
import { getResponseData } from './common.js';
export default class BlockService {
    db;
    userInfo;
    blockModel;
    constructor(dbpath) {
        this.db = new DateStore({
            filename: path.resolve(dbpath, 'database/blocks.db'),
            autoload: true,
        });
        this.db.ensureIndex({
            fieldName: 'label',
            unique: true,
        });
        this.userInfo = {
            id: 86,
            username: '开发者',
            email: 'developer@lowcode.com',
            resetPasswordToken: 'developer',
            confirmationToken: 'dfb2c162-351f-4f44-ad5f-8998',
            is_admin: true,
        };
        this.blockModel = {
            id: '',
            label: '',
            name_cn: '',
            framework: [],
            content: {},
            description: '',
            path: '',
            screenshot: '',
            created_app: '',
            tags: '',
            categories: [],
            occupier: {
                id: 86,
                username: '开发者',
                resetPasswordToken: 'developer',
            },
            isDefault: null,
            isOfficial: null,
        };
    }
    async create(params) {
        const blockData = { ...this.blockModel, ...params };
        const result = await this.db.insertAsync(blockData);
        const { _id } = result;
        await this.db.updateAsync({ _id }, { $set: { id: _id } });
        result.id = result._id;
        return result;
    }
    async update(id, params) {
        await this.db.updateAsync({ _id: id }, { $set: params });
        const result = await this.db.findOneAsync({ _id: id });
        return getResponseData(result);
    }
    async detail(blockId) {
        return this.db.findOneAsync({ _id: blockId });
    }
    async delete(blockId) {
        const result = await this.db.findOneAsync({ _id: blockId });
        await this.db.removeAsync({ _id: blockId });
        return getResponseData(result);
    }
    async list() {
        const result = await this.db.findAsync();
        return getResponseData(result);
    }
    async find(params) {
        return this.db.findAsync(params);
    }
    async findByLabel(label) {
        const result = await this.db.findOneAsync({ label });
        return getResponseData(result);
    }
}
