"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DocsMain = void 0;
function _harmony() {
  const data = require("@teambit/harmony");
  _harmony = function () {
    return data;
  };
  return data;
}
function _cli() {
  const data = require("@teambit/cli");
  _cli = function () {
    return data;
  };
  return data;
}
function _logger() {
  const data = require("@teambit/logger");
  _logger = function () {
    return data;
  };
  return data;
}
function _compiler() {
  const data = require("@teambit/compiler");
  _compiler = function () {
    return data;
  };
  return data;
}
function _component() {
  const data = require("@teambit/component");
  _component = function () {
    return data;
  };
  return data;
}
function _scope() {
  const data = require("@teambit/scope");
  _scope = function () {
    return data;
  };
  return data;
}
function _pkg() {
  const data = require("@teambit/pkg");
  _pkg = function () {
    return data;
  };
  return data;
}
function _graphql() {
  const data = require("@teambit/graphql");
  _graphql = function () {
    return data;
  };
  return data;
}
function _preview() {
  const data = require("@teambit/preview");
  _preview = function () {
    return data;
  };
  return data;
}
function _devFiles() {
  const data = require("@teambit/dev-files");
  _devFiles = function () {
    return data;
  };
  return data;
}
function _workspace() {
  const data = require("@teambit/workspace");
  _workspace = function () {
    return data;
  };
  return data;
}
function _docsEntities() {
  const data = require("@teambit/docs.entities.doc");
  _docsEntities = function () {
    return data;
  };
  return data;
}
function _lodash() {
  const data = require("lodash");
  _lodash = function () {
    return data;
  };
  return data;
}
function _envs() {
  const data = require("@teambit/envs");
  _envs = function () {
    return data;
  };
  return data;
}
function _docs() {
  const data = require("./docs.aspect");
  _docs = function () {
    return data;
  };
  return data;
}
function _docs2() {
  const data = require("./docs.preview-definition");
  _docs2 = function () {
    return data;
  };
  return data;
}
function _docs3() {
  const data = require("./docs.graphql");
  _docs3 = function () {
    return data;
  };
  return data;
}
function _defaultDocReader() {
  const data = require("./default-doc-reader");
  _defaultDocReader = function () {
    return data;
  };
  return data;
}
function _exceptions() {
  const data = require("./exceptions");
  _exceptions = function () {
    return data;
  };
  return data;
}
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
 * the component documentation extension.
 */
class DocsMain {
  constructor(patterns,
  /**
   * envs extension.
   */
  preview, pkg, compiler, workspace, logger, devFiles, envs, docPropSlot, docReaderSlot) {
    this.patterns = patterns;
    this.preview = preview;
    this.pkg = pkg;
    this.compiler = compiler;
    this.workspace = workspace;
    this.logger = logger;
    this.devFiles = devFiles;
    this.envs = envs;
    this.docPropSlot = docPropSlot;
    this.docReaderSlot = docReaderSlot;
  }

  /**
   * returns an array of doc file paths for a set of components.
   */
  getDocsMap(components) {
    return _component().ComponentMap.as(components, component => {
      return this.getDocsFiles(component);
    });
  }
  getDocsFiles(component) {
    const devFiles = this.devFiles.getDevFiles(component);
    const docFiles = devFiles.get(_docs().DocsAspect.id);
    return component.state.filesystem.files.filter(file => docFiles.includes(file.relative));
  }

  /**
   * compute the description of the component from its source code and docs file.
   */
  async getDescription(component) {
    const componentDoc = this.getDoc(component);
    const desc = componentDoc?.description;
    return desc || '';
  }
  async getTemplate(env) {
    if (env.getDocsTemplate && (0, _lodash().isFunction)(env.getDocsTemplate)) {
      return env.getDocsTemplate();
    }
    return undefined;
  }
  getDocReader(extension) {
    return this.docReaderSlot.values().find(docReader => docReader.isFormatSupported(extension));
  }

  /**
   * compute a doc for a component.
   */
  async computeDoc(component) {
    const docFiles = this.getDocsFiles(component);
    if (docFiles.length) {
      // currently taking the the first docs file found with an abstract. (we support only one)
      const docFile = docFiles[0];
      try {
        const docReader = this.getDocReader(docFile.extname);
        if (!docReader) throw new (_exceptions().FileExtensionNotSupported)(docFile.relative, docFile.extname);
        const doc = await docReader.read(docFile.relative, docFile.contents, component);
        return doc;
      } catch (err) {
        // it's ok to fail here.
        this.logger.debug(`docs.main.runtime.computeDoc caught an error: ${err.message}`);
        return null;
      }
    }
    return null;
  }
  getDoc(component) {
    const docData = component.get(_docs().DocsAspect.id)?.data?.doc;
    if (!docData) return null;
    return new (_docsEntities().Doc)(docData.filePath, new (_docsEntities().DocPropList)(docData.props));
  }
  getPatterns() {
    return this.patterns;
  }
  getComponentDevPatterns(component) {
    const env = this.envs.calculateEnv(component, {
      skipWarnings: !!this.workspace?.inInstallContext
    }).env;
    const componentEnvDocsDevPatterns = env.getDocsDevPatterns ? env.getDocsDevPatterns(component) : [];
    const componentPatterns = componentEnvDocsDevPatterns.concat(this.getPatterns());
    return {
      name: 'docs',
      pattern: componentPatterns
    };
  }
  getDevPatternToRegister() {
    return this.getComponentDevPatterns.bind(this);
  }
  /**
   * register a new doc reader. this allows to support further
   * documentation file formats.
   */
  registerDocReader(docReader) {
    this.docReaderSlot.register(docReader);
    return this;
  }
  static async provider([preview, graphql, workspace, pkg, compiler, loggerAspect, devFiles, envs, scope], config, [docPropSlot, docReaderSlot]) {
    const logger = loggerAspect.createLogger(_docs().DocsAspect.id);
    const docs = new DocsMain(config.patterns, preview, pkg, compiler, workspace, logger, devFiles, envs, docPropSlot, docReaderSlot);
    docs.registerDocReader(new (_defaultDocReader().DefaultDocReader)(pkg, compiler, workspace));
    devFiles.registerDevPattern(docs.getDevPatternToRegister());
    const computeDocsOnLoad = async (component, opts) => {
      if (opts?.loadDocs === false) return undefined;
      // const docFiles = await docs.computeDocs(component);
      const doc = await docs.computeDoc(component);
      return {
        doc: doc?.toObject()
        // docFiles
      };
    };
    if (workspace) {
      workspace.registerOnComponentLoad(computeDocsOnLoad);
    }
    if (scope) {
      scope.registerOnCompAspectReCalc(computeDocsOnLoad);
    }
    graphql.register((0, _docs3().docsSchema)(docs));
    preview.registerDefinition(new (_docs2().DocsPreviewDefinition)(docs));
    return docs;
  }
}
exports.DocsMain = DocsMain;
_defineProperty(DocsMain, "slots", [_harmony().Slot.withType(), _harmony().Slot.withType()]);
_defineProperty(DocsMain, "runtime", _cli().MainRuntime);
_defineProperty(DocsMain, "dependencies", [_preview().PreviewAspect, _graphql().GraphqlAspect, _workspace().WorkspaceAspect, _pkg().PkgAspect, _compiler().CompilerAspect, _logger().LoggerAspect, _devFiles().DevFilesAspect, _envs().EnvsAspect, _scope().ScopeAspect]);
_defineProperty(DocsMain, "defaultConfig", {
  patterns: ['**/*.docs.*']
});
_docs().DocsAspect.addRuntime(DocsMain);

//# sourceMappingURL=docs.main.runtime.js.map