"use strict";

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

var _lineByLine = require("./line-by-line");

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; }

/**
 * 
 * @format
 */

/**
 * External dependencies
 */

/**
 * Internal dependencies
 */
const SQL_DUMP_CREATE_TABLE_IS_MULTISITE_REGEX = /^CREATE TABLE `?(wp_\d_[a-z0-9_]*)/i;

function sqlDumpLineIsMultiSite(line) {
  // determine if we're on a CREATE TABLE statement line what has eg. wp_\d_options
  if (SQL_DUMP_CREATE_TABLE_IS_MULTISITE_REGEX.test(line)) {
    return true;
  }

  return false;
}

function isMultiSiteDumpFile(fileName) {
  return new Promise(async resolve => {
    const readInterface = await (0, _lineByLine.getReadInterface)(fileName);
    readInterface.on('line', line => {
      const result = sqlDumpLineIsMultiSite(line);

      if (true === result) {
        resolve(true);
      }
    });
    readInterface.on('error', () => {
      exit.withError('An error was encountered while reading your SQL dump file.  Please verify the file contents.');
    }); // Block until the processing completes

    await new Promise(resolveBlock => readInterface.on('close', resolveBlock));
    resolve(false);
  });
}