import path from 'path';
import DateStore from '@seald-io/nedb';
import { getResponseData } from './common.js';
export default class DataSourceService {
    db;
    userInfo;
    dataSourceModel;
    folderModel;
    constructor(dbpath) {
        this.db = new DateStore({
            filename: path.resolve(dbpath, 'database/datasources.db'),
            autoload: true,
        });
        this.db.ensureIndex({
            fieldName: 'name',
            unique: true,
        });
        this.userInfo = {
            id: 86,
            username: '开发者',
            email: 'developer@lowcode.com',
            resetPasswordToken: 'developer',
            confirmationToken: 'dfb2c162-351f-4f44-ad5f-8998',
            is_admin: true,
        };
        this.dataSourceModel = {
            id: '',
            name: '',
            app: '',
            data: {
                data: [],
                type: 'array',
                columns: [],
            },
            desc: '',
            options: {
                uri: 'api/api-permissions',
                method: 'GET',
            },
            dataHandler: {
                type: 'JSFunction',
                value: 'function dataHandler(data) { \n return data \n}',
            },
            willFetch: {
                type: 'JSFunction',
                value: 'function willFetch(option) {\n  return option \n}',
            },
            shouldFetch: {
                type: 'JSFunction',
                value: 'function shouldFetch(option) {\n  return true \n}',
            },
            errorHandler: {
                type: 'JSFunction',
                value: 'function errorHandler(err) {}',
            },
        };
        this.folderModel = {
            parentId: '0',
            route: 'test',
            name: 'test',
            app: '918',
            isPage: false,
            group: 'staticPages',
        };
    }
    async create(params) {
        const pageData = { ...this.dataSourceModel, ...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 findByName(name) {
        const result = await this.db.findOneAsync({ name });
        return getResponseData(result);
    }
    async delete(pageId) {
        const result = await this.db.findOneAsync({ _id: pageId });
        await this.db.removeAsync({ _id: pageId });
        return getResponseData(result);
    }
}
