text: "use strict";
  /**
   * Copyright Zendesk, Inc.
   *
   * Use of this source code is governed under the Apache License, Version 2.0
   * found at http://www.apache.org/licenses/LICENSE-2.0.
   */
  var __importDefault = (this && this.__importDefault) || function (mod) {
      return (mod && mod.__esModule) ? mod : { "default": mod };
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.execute = void 0;
  const commander_1 = require("commander");
  const __1 = require("..");
  const utils_1 = require("../../utils");
  const netlify_1 = __importDefault(require("netlify"));
  const node_fetch_1 = __importDefault(require("node-fetch"));
  /**
   * Execute the `netlify-bandwidth` command.
   *
   * @param {string} [args.token] Netlify personal access token.
   * @param {string} [args.siteId] Netlify site API ID.
   * @param {Ora} [args.spinner] Terminal spinner.
   *
   * @returns {object} The Netlify available and used bandwidth byte counts.
   */
  const execute = async (args = {}) => {
      let retVal;
      try {
          const token = args.token || (await (0, __1.token)(args.spinner));
          const client = new netlify_1.default(token);
          const siteId = args.siteId || (await (0, __1.siteId)(args.spinner));
          /* https://open-api.netlify.com/#operation/getSite */
          let response = await client.getSite({ siteId });
          const url = `${client.basePath}/accounts/${response.account_slug}/bandwidth`;
          /* bandwidth API call not yet supported by Netlify */
          response = await (0, node_fetch_1.default)(url, { headers: client.defaultHeaders });
          if (response.ok) {
              const data = await response.json();
              retVal = {
                  available: data.included + data.additional,
                  used: data.used
              };
          }
          else {
              throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
          }
      }
      catch (error) {
          (0, utils_1.handleErrorMessage)(error, 'netlify-bandwidth', args.spinner);
          throw error;
      }
      return retVal;
  };
  exports.execute = execute;
  exports.default = (spinner) => {
      const command = new commander_1.Command('netlify-bandwidth');
      return command
          .description('output remaining Netlify bandwidth')
          .option('-i, --id <id>', 'site API ID')
          .option('-t, --token <token>', 'access token')
          .action(async () => {
          try {
              spinner.start();
              const options = command.opts();
              const result = await (0, exports.execute)({
                  token: options.token,
                  siteId: options.id,
                  spinner
              });
              if (result) {
                  const bandwidth = `${result.available - result.used}`;
                  (0, utils_1.handleSuccessMessage)(bandwidth, spinner);
              }
              else {
                  throw new Error();
              }
          }
          catch {
              spinner.fail('Bandwidth not found');
              process.exitCode = 1;
          }
          finally {
              spinner.stop();
          }
      });
  };
  