import path from 'path';
import DateStore from '@seald-io/nedb';
import { getResponseData } from './common.js';
export default class PageService {
    db;
    userInfo;
    pageModel;
    folderModel;
    constructor(dbpath) {
        const __dirname = path.resolve();
        this.db = new DateStore({
            filename: path.resolve(dbpath, 'database/pages.db'),
            autoload: true,
        });
        this.db.ensureIndex({
            fieldName: 'route',
            unique: true,
        });
        this.userInfo = {
            id: 86,
            username: '开发者',
            email: 'developer@lowcode.com',
            resetPasswordToken: 'developer',
            confirmationToken: 'dfb2c162-351f-4f44-ad5f-8998',
            is_admin: true,
        };
        this.pageModel = {
            name: '',
            id: '',
            app: '918',
            route: '',
            page_content: {},
            tenant: 1,
            isBody: true,
            parentId: '',
            depth: 0,
            isPage: true,
            isDefault: false,
            group: 'staticPages',
            occupier: {
                id: 86,
                username: '开发者',
                email: 'developer@lowcode.com',
                resetPasswordToken: 'developer',
                confirmationToken: 'dfb2c162-351f-4f44-ad5f-8998',
                is_admin: true,
            },
        };
        this.folderModel = {
            parentId: '0',
            route: 'test',
            name: 'test',
            app: '918',
            isPage: false,
            group: 'staticPages',
        };
    }
    async create(params) {
        const model = params.isPage ? this.pageModel : this.folderModel;
        const pageData = { ...model, ...params };
        const result = await this.db.insertAsync(pageData);
        const { _id } = result;
        await this.db.updateAsync({ _id }, { $set: { id: _id } });
        result.id = result._id;
        return getResponseData(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 list(appId) {
        const result = await this.db.findAsync({ app: appId.toString() });
        return getResponseData(result);
    }
    async detail(pageId) {
        const result = await this.db.findOneAsync({ _id: pageId });
        return getResponseData(result);
    }
    async findByRoute(route) {
        const result = await this.db.findOneAsync({ route });
        return getResponseData(result);
    }
    async delete(pageId) {
        const result = await this.db.findOneAsync({ _id: pageId });
        await this.db.removeAsync({ _id: pageId });
        return getResponseData(result);
    }
}
