#!/usr/bin/env node

/**
 * 
 * @format
 */

/**
 * External dependencies
 */
"use strict";

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

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

var _dbFileImport = require("../lib/site-import/db-file-import");

var _status = require("../lib/site-import/status");

var _command = _interopRequireDefault(require("../lib/cli/command"));

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

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

/**
 * Internal dependencies
 */
const appQuery = `
id,
name,
type,
environments{
	id
	appId
	type
}
`;
(0, _command.default)({
  appContext: true,
  appQuery,
  envContext: true,
  requiredArgs: 0
}).argv(process.argv, async (arg, {
  app,
  env
}) => {
  const {
    id: envId,
    appId
  } = env;

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

  if (!(0, _dbFileImport.isSupportedApp)(app)) {
    await track('import_sql_command_error', {
      errorType: 'unsupported-app'
    });
    exit.withError('The type of application you specified does not currently support SQL imports.');
  }

  await track('import_sql_check_status_command_execute');
  console.log(`Checking the sql import status for env ID: ${env.id}, app ID: ${env.appId}`);
  await (0, _status.importSqlCheckStatus)({
    app,
    env
  });
});