text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const config_1 = require("../../../../config");
  const request_1 = require("../../../../request");
  const commands_1 = require("../../commands");
  const Command_1 = require("../../../../Command");
  const SpoCommand_1 = require("../../../base/SpoCommand");
  const Utils_1 = require("../../../../Utils");
  const vorpal = require('../../../../vorpal-init');
  class SpoStorageEntityRemoveCommand extends SpoCommand_1.default {
      get name() {
          return `${commands_1.default.STORAGEENTITY_REMOVE}`;
      }
      get description() {
          return 'Removes tenant property stored on the specified SharePoint Online app catalog';
      }
      getTelemetryProperties(args) {
          const telemetryProps = super.getTelemetryProperties(args);
          telemetryProps.confirm = (!(!args.options.confirm)).toString();
          return telemetryProps;
      }
      commandAction(cmd, args, cb) {
          const removeTenantProperty = () => {
              let spoAdminUrl = '';
              if (this.verbose) {
                  cmd.log(`Removing tenant property ${args.options.key} from ${args.options.appCatalogUrl}...`);
              }
              this
                  .getSpoAdminUrl(cmd, this.debug)
                  .then((_spoAdminUrl) => {
                  spoAdminUrl = _spoAdminUrl;
                  return this.getRequestDigest(spoAdminUrl);
              })
                  .then((res) => {
                  const requestOptions = {
                      url: `${spoAdminUrl}/_vti_bin/client.svc/ProcessQuery`,
                      headers: {
                          'X-RequestDigest': res.FormDigestValue
                      },
                      body: `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config_1.default.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="31" ObjectPathId="30" /><ObjectPath Id="33" ObjectPathId="32" /><ObjectPath Id="35" ObjectPathId="34" /><Method Name="RemoveStorageEntity" Id="36" ObjectPathId="34"><Parameters><Parameter Type="String">${Utils_1.default.escapeXml(args.options.key)}</Parameter></Parameters></Method></Actions><ObjectPaths><Constructor Id="30" TypeId="{268004ae-ef6b-4e9b-8425-127220d84719}" /><Method Id="32" ParentId="30" Name="GetSiteByUrl"><Parameters><Parameter Type="String">${Utils_1.default.escapeXml(args.options.appCatalogUrl)}</Parameter></Parameters></Method><Property Id="34" ParentId="32" Name="RootWeb" /></ObjectPaths></Request>`
                  };
                  return request_1.default.post(requestOptions);
              })
                  .then((res) => {
                  const json = JSON.parse(res);
                  const response = json[0];
                  if (response.ErrorInfo) {
                      cb(new Command_1.CommandError(response.ErrorInfo.ErrorMessage));
                  }
                  else {
                      if (this.verbose) {
                          cmd.log(vorpal.chalk.green('DONE'));
                      }
                      cb();
                  }
              }, (err) => this.handleRejectedPromise(err, cmd, cb));
          };
          if (args.options.confirm) {
              removeTenantProperty();
          }
          else {
              cmd.prompt({
                  type: 'confirm',
                  name: 'continue',
                  default: false,
                  message: `Are you sure you want to delete the ${args.options.key} tenant property?`,
              }, (result) => {
                  if (!result.continue) {
                      cb();
                  }
                  else {
                      removeTenantProperty();
                  }
              });
          }
      }
      options() {
          const options = [
              {
                  option: '-u, --appCatalogUrl <appCatalogUrl>',
                  description: 'URL of the app catalog site'
              },
              {
                  option: '-k, --key <key>',
                  description: 'Name of the tenant property to retrieve'
              },
              {
                  option: '--confirm',
                  description: 'Don\'t prompt for confirming removal of a tenant property'
              }
          ];
          const parentOptions = super.options();
          return options.concat(parentOptions);
      }
      validate() {
          return (args) => {
              const result = SpoCommand_1.default.isValidSharePointUrl(args.options.appCatalogUrl);
              if (result === false) {
                  return 'Missing required option appCatalogUrl';
              }
              else {
                  return result;
              }
          };
      }
      commandHelp(args, log) {
          const chalk = vorpal.chalk;
          log(vorpal.find(commands_1.default.STORAGEENTITY_REMOVE).helpInformation());
          log(`  ${chalk.yellow('Important:')} to use this command you have to have permissions to access
      the tenant admin site.
      
    Remarks:
  
      Tenant properties are stored in the app catalog site associated with that
      tenant. To remove a property, you have to specify the absolute URL of the
      app catalog site. If you specify the URL of a site different than the app
      catalog, you will get an access denied error.
  
    Examples:
    
      Remove the ${chalk.grey('AnalyticsId')} tenant property. Yields a confirmation prompt
      before actually removing the property
        ${commands_1.default.STORAGEENTITY_REMOVE} --key AnalyticsId --appCatalogUrl https://contoso.sharepoint.com/sites/appcatalog
  
      Remove the ${chalk.grey('AnalyticsId')} tenant property. Suppresses the confirmation
      prompt
        ${commands_1.default.STORAGEENTITY_REMOVE} --key AnalyticsId --confirm --appCatalogUrl https://contoso.sharepoint.com/sites/appcatalog
  
    More information:
  
      SharePoint Framework Tenant Properties
        https://docs.microsoft.com/en-us/sharepoint/dev/spfx/tenant-properties
  `);
      }
  }
  module.exports = new SpoStorageEntityRemoveCommand();
  //# sourceMappingURL=storageentity-remove.js.map