text: "use strict";
  Object.defineProperty(exports, "__esModule", { value: true });
  const config_1 = require("../../../../config");
  const request_1 = require("../../../../request");
  const uuidv4 = require('uuid/v4');
  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 SpoTermSetAddCommand extends SpoCommand_1.default {
      get name() {
          return commands_1.default.TERM_SET_ADD;
      }
      get description() {
          return 'Adds taxonomy term set';
      }
      getTelemetryProperties(args) {
          const telemetryProps = super.getTelemetryProperties(args);
          telemetryProps.customProperties = typeof args.options.customProperties !== 'undefined';
          telemetryProps.description = typeof args.options.description !== 'undefined';
          telemetryProps.id = typeof args.options.id !== 'undefined';
          telemetryProps.termGroupId = typeof args.options.termGroupId !== 'undefined';
          telemetryProps.termGroupName = typeof args.options.termGroupName !== 'undefined';
          return telemetryProps;
      }
      commandAction(cmd, args, cb) {
          let formDigest = '';
          let termSet;
          let spoAdminUrl = '';
          this
              .getSpoAdminUrl(cmd, this.debug)
              .then((_spoAdminUrl) => {
              spoAdminUrl = _spoAdminUrl;
              return this.getRequestDigest(spoAdminUrl);
          })
              .then((res) => {
              formDigest = res.FormDigestValue;
              if (this.verbose) {
                  cmd.log(`Adding taxonomy term set...`);
              }
              const termGroupQuery = args.options.termGroupName ?
                  `<Method Id="42" ParentId="40" Name="GetByName"><Parameters><Parameter Type="String">${Utils_1.default.escapeXml(args.options.termGroupName)}</Parameter></Parameters></Method>` :
                  `<Method Id="42" ParentId="40" Name="GetById"><Parameters><Parameter Type="Guid">{${args.options.termGroupId}}</Parameter></Parameters></Method>`;
              const termSetId = args.options.id || uuidv4();
              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="35" ObjectPathId="34" /><ObjectIdentityQuery Id="36" ObjectPathId="34" /><ObjectPath Id="38" ObjectPathId="37" /><ObjectIdentityQuery Id="39" ObjectPathId="37" /><ObjectPath Id="41" ObjectPathId="40" /><ObjectPath Id="43" ObjectPathId="42" /><ObjectIdentityQuery Id="44" ObjectPathId="42" /><ObjectPath Id="46" ObjectPathId="45" /><ObjectIdentityQuery Id="47" ObjectPathId="45" /><Query Id="48" ObjectPathId="45"><Query SelectAllProperties="true"><Properties /></Query></Query></Actions><ObjectPaths><StaticMethod Id="34" Name="GetTaxonomySession" TypeId="{981cbc68-9edc-4f8d-872f-71146fcbb84f}" /><Method Id="37" ParentId="34" Name="GetDefaultSiteCollectionTermStore" /><Property Id="40" ParentId="37" Name="Groups" />${termGroupQuery}<Method Id="45" ParentId="42" Name="CreateTermSet"><Parameters><Parameter Type="String">${Utils_1.default.escapeXml(args.options.name)}</Parameter><Parameter Type="Guid">{${termSetId}}</Parameter><Parameter Type="Int32">1033</Parameter></Parameters></Method></ObjectPaths></Request>`
              };
              return request_1.default.post(requestOptions);
          })
              .then((res) => {
              const json = JSON.parse(res);
              const response = json[0];
              if (response.ErrorInfo) {
                  return Promise.reject(response.ErrorInfo.ErrorMessage);
              }
              termSet = json[json.length - 1];
              if (!args.options.description &&
                  !args.options.customProperties) {
                  return Promise.resolve(undefined);
              }
              let termStoreObjectIdentity = '';
              // get term store object identity
              for (let i = 0; i < json.length; i++) {
                  if (json[i] !== 39) {
                      continue;
                  }
                  termStoreObjectIdentity = json[i + 1]._ObjectIdentity_;
                  break;
              }
              if (this.verbose) {
                  cmd.log(`Setting term set properties...`);
              }
              const properties = [];
              let i = 127;
              if (args.options.description) {
                  properties.push(`<SetProperty Id="${i++}" ObjectPathId="117" Name="Description"><Parameter Type="String">${Utils_1.default.escapeXml(args.options.description)}</Parameter></SetProperty>`);
                  termSet.Description = args.options.description;
              }
              if (args.options.customProperties) {
                  const customProperties = JSON.parse(args.options.customProperties);
                  Object.keys(customProperties).forEach(k => {
                      properties.push(`<Method Name="SetCustomProperty" Id="${i++}" ObjectPathId="117"><Parameters><Parameter Type="String">${Utils_1.default.escapeXml(k)}</Parameter><Parameter Type="String">${Utils_1.default.escapeXml(customProperties[k])}</Parameter></Parameters></Method>`);
                  });
                  termSet.CustomProperties = customProperties;
              }
              const requestOptions = {
                  url: `${spoAdminUrl}/_vti_bin/client.svc/ProcessQuery`,
                  headers: {
                      'X-RequestDigest': formDigest
                  },
                  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>${properties.join('')}<Method Name="CommitAll" Id="131" ObjectPathId="109" /></Actions><ObjectPaths><Identity Id="117" Name="${termSet._ObjectIdentity_}" /><Identity Id="109" Name="${termStoreObjectIdentity}" /></ObjectPaths></Request>`
              };
              return request_1.default.post(requestOptions);
          })
              .then((res) => {
              if (res) {
                  const json = JSON.parse(res);
                  const response = json[0];
                  if (response.ErrorInfo) {
                      cb(new Command_1.CommandError(response.ErrorInfo.ErrorMessage));
                      return;
                  }
              }
              delete termSet._ObjectIdentity_;
              delete termSet._ObjectType_;
              termSet.CreatedDate = new Date(Number(termSet.CreatedDate.replace('/Date(', '').replace(')/', ''))).toISOString();
              termSet.Id = termSet.Id.replace('/Guid(', '').replace(')/', '');
              termSet.LastModifiedDate = new Date(Number(termSet.LastModifiedDate.replace('/Date(', '').replace(')/', ''))).toISOString();
              cmd.log(termSet);
              cb();
          }, (err) => this.handleRejectedPromise(err, cmd, cb));
      }
      options() {
          const options = [
              {
                  option: '-n, --name <name>',
                  description: 'Name of the term set to add'
              },
              {
                  option: '--termGroupId [termGroupId]',
                  description: 'ID of the term group in which to create the term set. Specify termGroupId or termGroupName but not both'
              },
              {
                  option: '--termGroupName [termGroupName]',
                  description: 'Name of the term group in which to create the term set. Specify termGroupId or termGroupName but not both'
              },
              {
                  option: '-i, --id [id]',
                  description: 'ID of the term set to add'
              },
              {
                  option: '-d, --description [description]',
                  description: 'Description of the term set to add'
              },
              {
                  option: '--customProperties [customProperties]',
                  description: 'JSON string with key-value pairs representing custom properties to set on the term set'
              }
          ];
          const parentOptions = super.options();
          return options.concat(parentOptions);
      }
      validate() {
          return (args) => {
              if (!args.options.name) {
                  return 'Required option name missing';
              }
              if (args.options.id) {
                  if (!Utils_1.default.isValidGuid(args.options.id)) {
                      return `${args.options.id} is not a valid GUID`;
                  }
              }
              if (!args.options.termGroupId && !args.options.termGroupName) {
                  return 'Specify termGroupId or termGroupName';
              }
              if (args.options.termGroupId && args.options.termGroupName) {
                  return 'Specify termGroupId or termGroupName but not both';
              }
              if (args.options.termGroupId) {
                  if (!Utils_1.default.isValidGuid(args.options.termGroupId)) {
                      return `${args.options.termGroupId} is not a valid GUID`;
                  }
              }
              if (args.options.customProperties) {
                  try {
                      JSON.parse(args.options.customProperties);
                  }
                  catch (e) {
                      return `Error when parsing customProperties JSON: ${e}`;
                  }
              }
              return true;
          };
      }
      commandHelp(args, log) {
          const chalk = vorpal.chalk;
          log(vorpal.find(commands_1.default.TERM_SET_ADD).helpInformation());
          log(`  ${chalk.yellow('Important:')} to use this command you have to have permissions to access
      the tenant admin site.
      
    Examples:
    
      Add taxonomy term set to the term group specified by ID
        ${commands_1.default.TERM_SET_ADD} --name PnP-Organizations --termGroupId 0e8f395e-ff58-4d45-9ff7-e331ab728beb
  
      Add taxonomy term set to the term group specified by name. Create the term
      set with the specified ID
        ${commands_1.default.TERM_SET_ADD} --name PnP-Organizations --termGroupName PnPTermSets --id aa70ede6-83d1-466d-8d95-30d29e9bbd7c
  
      Add taxonomy term set and set its description
        ${commands_1.default.TERM_SET_ADD} --name PnP-Organizations --termGroupId 0e8f395e-ff58-4d45-9ff7-e331ab728beb --description 'Contains a list of organizations'
  
      Add taxonomy term set and set its custom properties
        ${commands_1.default.TERM_SET_ADD} --name PnP-Organizations --termGroupId 0e8f395e-ff58-4d45-9ff7-e331ab728beb --customProperties '\`{"Property":"Value"}\`'
  `);
      }
  }
  module.exports = new SpoTermSetAddCommand();
  //# sourceMappingURL=term-set-add.js.map