import { DocumentState } from "langium";
import { isParsedLikeC4LangiumDocument, ViewOps } from "../ast.js";
import { logger } from "../logger.js";
export class LikeC4CodeLensProvider {
  constructor(services) {
    this.services = services;
  }
  async provideCodeLens(doc, _params, cancelToken) {
    if (doc.state !== DocumentState.Validated) {
      logger.debug(`Waiting for document ${doc.uri.path} to be validated`);
      await this.services.shared.workspace.DocumentBuilder.waitUntil(DocumentState.Validated, doc.uri, cancelToken);
      logger.debug(`Document ${doc.uri.path} is validated`);
    }
    if (!isParsedLikeC4LangiumDocument(doc)) {
      return;
    }
    const views = doc.parseResult.value.views.flatMap((v) => v.views);
    return views.flatMap((ast) => {
      const viewId = ViewOps.readId(ast);
      const range = ast.$cstNode?.range;
      if (!range || !viewId) {
        return [];
      }
      return {
        range: {
          start: range.start,
          end: {
            line: range.start.line,
            character: range.start.character + 4
          }
        },
        command: {
          command: "likec4.open-preview",
          arguments: [viewId],
          title: "open preview"
        }
      };
    });
  }
}
