"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getExecutablePath = getExecutablePath;
exports.getTextFromImage = getTextFromImage;
exports.isSupportedFile = isSupportedFile;
var _child_process = require("child_process");
var _path = _interopRequireDefault(require("path"));
var _tar = require("tar");
var _isImage = _interopRequireDefault(require("is-image"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const unsupportedExtensions = new Set(['ai', 'emf', 'eps', 'gif', 'ico', 'psd', 'svg']);
const inputPath = _path.default.join(__dirname, '..', 'bin', 'tt.tar.gz');
const outputPath = '/tmp/tesseract-standalone/tesseract';
async function getExecutablePath() {
  await (0, _tar.extract)({
    file: inputPath,
    cwd: '/tmp'
  });
  return outputPath;
}
async function getTextFromImage(filePath) {
  await (0, _tar.extract)({
    file: inputPath,
    cwd: '/tmp'
  });
  const stdout = (0, _child_process.execFileSync)(outputPath, [filePath, 'stdout', '-l', 'eng'], {
    cwd: '/tmp/tesseract-standalone',
    env: {
      LD_LIBRARY_PATH: './lib',
      TESSDATA_PREFIX: './tessdata'
    }
  });
  (0, _child_process.execSync)(`rm ${filePath}`);
  return stdout.toString();
}
function isSupportedFile(filePath) {
  // Reject all non-image files for OCR
  if (!(0, _isImage.default)(filePath)) {
    return false;
  }
  return !isUnsupportedFileExtension(filePath);
}
function isUnsupportedFileExtension(filePath) {
  const ext = _path.default.extname(filePath).slice(1).toLowerCase();
  return unsupportedExtensions.has(ext);
}