"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getReadInterface = getReadInterface;
exports.fileLineValidations = fileLineValidations;

var _readline = _interopRequireDefault(require("readline"));

var _fs = _interopRequireDefault(require("fs"));

var _chalk = _interopRequireDefault(require("chalk"));

var _debug = _interopRequireDefault(require("debug"));

var exit = _interopRequireWildcard(require("../cli/exit"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * 
 * @format
 */

/**
 * External dependencies
 */

/**
 * Internal dependencies
 */
const debug = (0, _debug.default)('vip:validations:line-by-line');

function openFile(filename, flags = 'r', mode = 666) {
  return new Promise((resolve, reject) => {
    _fs.default.open(filename, flags, mode, (err, fd) => {
      if (err) {
        return reject(err);
      }

      resolve(fd);
    });
  });
}

async function getReadInterface(filename) {
  let fd;

  try {
    fd = await openFile(filename);
  } catch (e) {
    exit.withError('The file at the provided path is either missing or not readable. Please check the input and try again.');
  }

  return _readline.default.createInterface({
    input: _fs.default.createReadStream('', {
      fd
    }),
    output: null,
    console: false
  });
}

async function fileLineValidations(appId, envId, fileName, validations) {
  const isImport = true;
  const readInterface = await getReadInterface(fileName);
  debug('Validations: ', validations);
  readInterface.on('line', line => {
    validations.map(validation => {
      validation.execute(line);
    });
  });
  readInterface.on('error', err => {
    throw new Error(` Error validating input file: ${err.toString()}`);
  }); // Block until the processing completes

  await new Promise(resolve => readInterface.on('close', resolve));
  readInterface.close();
  await Promise.all(validations.map(async validation => {
    if (validation.hasOwnProperty('postLineExecutionProcessing') && typeof validation.postLineExecutionProcessing === 'function') {
      await validation.postLineExecutionProcessing({
        fileName,
        isImport,
        appId,
        envId
      });
    }
  }));
}