"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
exports.__esModule = true;
exports.parse = exports.namespace = exports.mediaTypes = exports.detect = void 0;
var _browser = _interopRequireDefault(require("./lexical-analysis/browser.cjs"));
exports.lexicalAnalysis = _browser.default;
var _index = _interopRequireDefault(require("./syntactic-analysis/direct/index.cjs"));
exports.syntacticAnalysis = _index.default;
exports.syntacticAnalysisDirect = _index.default;
var _index2 = _interopRequireDefault(require("./syntactic-analysis/indirect/index.cjs"));
exports.syntacticAnalysisIndirect = _index2.default;
var _adapter = require("./adapter.cjs");
exports.detectionRegExp = _adapter.detectionRegExp;
exports.mediaTypes = _adapter.mediaTypes;
exports.namespace = _adapter.namespace;
/**
 * @public
 */
const detect = async source => {
  if (!_adapter.detectionRegExp.test(source)) {
    return false;
  }
  let cst = null;
  try {
    cst = await (0, _browser.default)(source);
    const isError = cst.rootNode.type !== 'ERROR';
    return isError;
  } catch {
    return false;
  } finally {
    if (cst !== null) {
      cst.delete();
    }
  }
};

/**
 * @public
 */

/**
 * @public
 */
exports.detect = detect;
/**
 * @public
 */
const parse = async (source, {
  sourceMap = false,
  syntacticAnalysis = 'direct'
} = {}) => {
  let cst = null;
  try {
    cst = await (0, _browser.default)(source);
    if (syntacticAnalysis === 'indirect') {
      return (0, _index2.default)(cst, {
        sourceMap
      });
    }
    return (0, _index.default)(cst, {
      sourceMap
    });
  } finally {
    if (cst !== null) {
      cst.delete();
    }
  }
};
exports.parse = parse;