import lexicalAnalysis from "./lexical-analysis/browser.mjs";
import syntacticAnalysisDirect from "./syntactic-analysis/direct/index.mjs";
import syntacticAnalysisIndirect from "./syntactic-analysis/indirect/index.mjs";
import { detectionRegExp } from "./adapter.mjs";
export { mediaTypes, namespace } from "./adapter.mjs";
export { detectionRegExp };
export { lexicalAnalysis, syntacticAnalysisDirect as syntacticAnalysis, syntacticAnalysisDirect, syntacticAnalysisIndirect };

/**
 * @public
 */
export const detect = async source => {
  if (!detectionRegExp.test(source)) {
    return false;
  }
  let cst = null;
  try {
    cst = await lexicalAnalysis(source);
    const isError = cst.rootNode.type !== 'ERROR';
    return isError;
  } catch {
    return false;
  } finally {
    if (cst !== null) {
      cst.delete();
    }
  }
};

/**
 * @public
 */

/**
 * @public
 */

/**
 * @public
 */
export const parse = async (source, {
  sourceMap = false,
  syntacticAnalysis = 'direct'
} = {}) => {
  let cst = null;
  try {
    cst = await lexicalAnalysis(source);
    if (syntacticAnalysis === 'indirect') {
      return syntacticAnalysisIndirect(cst, {
        sourceMap
      });
    }
    return syntacticAnalysisDirect(cst, {
      sourceMap
    });
  } finally {
    if (cst !== null) {
      cst.delete();
    }
  }
};