import type { BlockSchema, ProjectSchema } from '@vtj/core';

import { getFile as getLowCodeFile, getProject } from '@/io';
import { ProjectModel } from '@vtj/core';
import { BaseService } from '@vtj/renderer';

export class LowCodeService extends BaseService {
  public async getFile(id: string): Promise<BlockSchema> {
    return getLowCodeFile(id).then((lowCodeFile) => {
      return lowCodeFile.published_dsl
        ? Promise.resolve(lowCodeFile.published_dsl as BlockSchema)
        : Promise.reject(new Error(`dsl 不存在, id: ${id}`));
    });
  }

  public async init(project: ProjectSchema) {
    // @ts-ignore 忽略错误
    const remoteProject = await getProject(project.id);
    const projectModel = new ProjectModel(remoteProject);
    const projectDSL = projectModel.toDsl();
    console.log('project dsl', projectDSL);
    return projectDSL;
  }
}
