"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.siteTypeValidations = void 0;

var _tracker = require("../tracker");

var _isMultiSiteSqlDump = require("./is-multi-site-sql-dump");

var _isMultiSite = require("./is-multi-site");

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
 */
let isMultiSiteSqlDump = false;
const siteTypeValidations = {
  execute: line => {
    const lineIsMultiSite = (0, _isMultiSiteSqlDump.sqlDumpLineIsMultiSite)(line);

    if (lineIsMultiSite) {
      isMultiSiteSqlDump = true;
    }
  },
  postLineExecutionProcessing: async ({
    appId,
    envId
  }) => {
    const isMultiSite = await (0, _isMultiSite.isMultiSiteInSiteMeta)(appId, envId);

    const track = _tracker.trackEventWithEnv.bind(null, appId, envId);

    console.log(`\nAppId: ${appId} is ${isMultiSite ? 'a multisite.' : 'not a multisite'}`);
    console.log(`The SQL dump provided is ${isMultiSiteSqlDump ? 'from a multisite.' : 'not from a multisite'}\n`); // if site is a multisite but import sql is not

    if (isMultiSite && !isMultiSiteSqlDump) {
      await track('import_sql_command_error', {
        error_type: 'multisite-but-not-multisite-sql-dump'
      });
      exit.withError('You have provided a non-multisite SQL dump file for import into a multisite.');
    } // if site is a single site but import sql is for a multi site


    if (!isMultiSite && isMultiSiteSqlDump) {
      await track('import_sql_command_error', {
        error_type: 'not-multisite-with-multisite-sql-dump'
      });
      exit.withError('You have provided a multisite SQL dump file for import into a single site (non-multisite).');
    }
  }
};
exports.siteTypeValidations = siteTypeValidations;