text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const commands_1 = require("../../commands");
  const Command_1 = require("../../../../Command");
  const fs = require("fs");
  const path = require("path");
  const os = require("os");
  const AnonymousCommand_1 = require("../../../base/AnonymousCommand");
  const autocomplete_1 = require("../../../../autocomplete");
  const vorpal = require('../../../../vorpal-init');
  class CliCompletionPwshSetupCommand extends AnonymousCommand_1.default {
      get name() {
          return commands_1.default.COMPLETION_PWSH_SETUP;
      }
      get description() {
          return 'Sets up command completion for PowerShell';
      }
      commandAction(cmd, args, cb) {
          if (this.debug) {
              cmd.log('Generating command completion...');
          }
          autocomplete_1.autocomplete.generateShCompletion(vorpal);
          if (this.debug) {
              cmd.log(`Ensuring that the specified profile path ${args.options.profile} exists...`);
          }
          if (fs.existsSync(args.options.profile)) {
              if (this.debug) {
                  cmd.log('Profile file already exists');
              }
          }
          else {
              // check if the path exists
              const dirname = path.dirname(args.options.profile);
              if (fs.existsSync(dirname)) {
                  if (this.debug) {
                      cmd.log(`Profile path ${dirname} already exists`);
                  }
              }
              else {
                  try {
                      if (this.debug) {
                          cmd.log(`Profile path ${dirname} doesn't exist. Creating...`);
                      }
                      fs.mkdirSync(dirname, { recursive: true });
                  }
                  catch (e) {
                      cb(new Command_1.CommandError(e));
                      return;
                  }
              }
              if (this.debug) {
                  cmd.log(`Creating profile file ${args.options.profile}...`);
              }
              try {
                  fs.writeFileSync(args.options.profile, '', 'utf8');
              }
              catch (e) {
                  cb(new Command_1.CommandError(e));
                  return;
              }
          }
          if (this.verbose) {
              cmd.log(`Adding Office 365 CLI command completion to PowerShell profile...`);
          }
          const completionScriptPath = path.resolve(__dirname, '..', '..', '..', '..', '..', 'scripts', 'Register-O365CLICompletion.ps1');
          try {
              fs.appendFileSync(args.options.profile, os.EOL + completionScriptPath, 'utf8');
              if (this.verbose) {
                  cmd.log(vorpal.chalk.green('DONE'));
              }
              cb();
          }
          catch (e) {
              cb(new Command_1.CommandError(e));
          }
      }
      options() {
          const options = [
              {
                  option: '-p, --profile <profile>',
                  description: 'Path to the PowerShell profile file'
              }
          ];
          const parentOptions = super.options();
          return options.concat(parentOptions);
      }
      validate() {
          return (args) => {
              if (!args.options.profile) {
                  return 'Required option profile missing';
              }
              return true;
          };
      }
      commandHelp(args, log) {
          const chalk = vorpal.chalk;
          log(vorpal.find(commands_1.default.COMPLETION_PWSH_SETUP).helpInformation());
          log(`  Remarks:
    
      This commands sets up command completion for the Office 365 CLI in
      PowerShell by registering a custom PowerShell argument completer
      in the specified profile. Because Office 365 CLI is not a native PowerShell
      module, it requires a custom completer to provide completion when used in
      non-immersive mode.
      
      If the specified profile path doesn't exist, the CLI will try to create it.
     
    Examples:
    
      Set up command completion for PowerShell using the profile from the ${chalk.grey('profile')}
      variable
        ${this.getCommandName()} --profile $profile
  
    More information:
  
      Command completion
        https://pnp.github.io/office365-cli/concepts/completion/
  `);
      }
  }
  module.exports = new CliCompletionPwshSetupCommand();
  //# sourceMappingURL=completion-pwsh-setup.js.map