/*** Generated by streamline 0.10.17 (callbacks) - DO NOT EDIT ***/ "use strict"; var __rt=require('streamline/lib/callbacks/runtime').runtime(__filename, false),__func=__rt.__func,__cb=__rt.__cb,__catch=__rt.__catch,__tryCatch=__rt.__tryCatch; var adUtils = require("./adUtils");
















var profile = require("../../../util/profile");
var utils = require("../../../util/utils");
var util = require("util");
var moment = require("moment");

var $ = utils.getLocaleString;

exports.init = function(cli) {
  var log = cli.output;
  var withProgress = cli.interaction.withProgress.bind(cli.interaction);

  var ad = cli.category("ad").description($("Commands to display Active Directory objects"));

  var adApp = ad.category("app").description($("Commands to display Active Directory applications"));


  adApp.command("create [name] [home-page] [identifier-uris]").description($("Creates a new Active Directory application")).usage("[options] <name> <home-page> <identifier-uris>").option("-n --name <name>", $("the display name for the application")).option("-m --home-page <home-page>", $("the URL to the application homepage")).option("-a --available", $("indicates if the application will be available to other tenants")).option("-p --password <password>", $("the value for the password credential associated with the application that will be valid for one year by default")).option("-i --identifier-uris <identifier-uris>", $("the comma-delimitied URIs that identify the application")).option("-r --reply-urls <reply-urls>", $("the comma-delimitied application reply urls")).option("--key-value <key-value>", $("the value for the key credentials associated with the application that will be valid for one year by default")).option("--key-type <key-type>", $("the type of the key credentials associated with the application. Acceptable values are AsymmetricX509Cert, Password and Symmetric")).option("--key-usage <key-usage>", $("the usage of the key credentials associated with the application. Acceptable values are Sign and Verify")).option("--start-date <start-date>", $("the start date after which password or key would be valid. Default value is current time")).option("--end-date <end-date>", $("the end date till which password or key is valid. Default value is one year after current time")).execute(function __1(name, homePage, identifierUris, options, _) { var application; var __frame = { name: "__1", line: 48 }; return __func(_, this, arguments, __1, 4, __frame, function __$__1() {














      return adUtils.createApplication(cli, name, homePage, identifierUris, options, __cb(_, __frame, 1, 32, function ___(__0, __1) { application = __1;
        cli.interaction.formatOutput(application, function(data) {
          if (data) {
            adUtils.displayAApplication(data, log); } ; }); _(); }, true)); }); });




  adApp.command("set").description($("Updates the properties of the created Active Directory application")).option("-o --objectId <objectId>", $("the object id of the application to update. Either provide objectId or applicationId. objectId is more preferable.")).option("-a --applicationId <applicationId>", $("the applicationId of the application to update. If you have created a service principal using this application then this will be the spn of the service principal.")).option("-n --name <name>", $("the new display name for the application.")).option("-m --home-page <home-page>", $("the new URL to the application homepage")).option("-p --password <password>", $("new value for the password credential associated with the application that will be valid for one year by default")).option("-i --identifier-uris <identifier-uris>", $("new comma-delimitied URI values that identify the application")).option("-r --reply-urls <reply-urls>", $("new comma-delimitied application reply urls")).option("--key-value <key-value>", $("new value for the key credentials associated with the application that will be valid for one year by default")).option("--key-type <key-type>", $("new type of the key credentials associated with the application. Acceptable values are AsymmetricX509Cert, Password and Symmetric. Default value: AsymmetricX509Cert")).option("--key-usage <key-usage>", $("new usage of the key credentials associated with the application. Acceptable values are Sign and Verify. Default value: Verify")).option("--start-date <start-date>", $("new start date value after which password or key would be valid. Default value is current time")).option("--end-date <end-date>", $("new end date value till which password or key is valid. Default value is one year after current time")).execute(function __2(options, _) { var applicationId, objectId, startDate, endDate, keyType, keyUsage, appParams, subscription, client, application, applications, currentUserObject, parameters; var __frame = { name: "__2", line: 71 }; return __func(_, this, arguments, __2, 1, __frame, function __$__2() {















      applicationId = options.applicationId;
      objectId = options.objectId;
      startDate = (options.startDate ? new Date(Date.parse(options.startDate)) : new Date(Date.now()));










      return (function ___closure(_) { var date, m; if (options.endDate) { return _(null, new Date(Date.parse(options.endDate))); } else { date = new Date(startDate); m = moment(date); m.add(1, "years"); date = new Date(m.toISOString()); return _(null, date); } ; _(); })(__cb(_, __frame, 5, 20, function ___(__0, __2) { endDate = __2;

        keyType = (options.keyType ? options.keyType : "AsymmetricX509Cert");
        keyUsage = (options.keyUsage ? options.keyUsage : "Verify");

        appParams = { };
        if (options) {
          if (options.available) {
            appParams.availableToOtherTenants = true; } ;

          if (options.name) {
            appParams.displayName = options.name; } ;


          if (options.homePage) {
            appParams.homepage = options.homePage; } ;


          if (options.identifierUris) {
            appParams.identifierUris = options.identifierUris.split(","); } ;


          if (options.replyUrls) {
            appParams.replyUrls = options.replyUrls.split(","); } ;


          if (options.password) {
            appParams.passwordCredentials = [{
              startDate: startDate,
              endDate: endDate,
              keyId: utils.uuidGen(),
              value: options.password },]; } ;



          if (options.keyValue) {
            appParams.keyCredentials = [{
              startDate: startDate,
              endDate: endDate,
              keyId: utils.uuidGen(),
              value: options.keyValue,
              usage: keyUsage,
              type: keyType },]; } ; } ;




        subscription = profile.current.getSubscription(options.subscription);
        client = adUtils.getADGraphClient(subscription);

        application = null;
        applications = []; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__2() { return (function __$__2(__then) {

                if ((applicationId && !objectId)) {
                  parameters = { filter: (("appId eq '" + applicationId) + "'") };
                  return client.applications.list(parameters, __cb(_, __frame, 70, 45, function ___(__0, __4) { applications = __4;
                    if ((!applications || ((applications && (applications.length === 0))))) {
                      return _(new Error($(util.format(("The provided applicationId: '%s' was not found. Hence the application cannot be updated. " + "If you are not sure, then please execute 'azure ad app list' and find out the correct applicationId or objectId."), applicationId)))); } ;


                    objectId = applications[0].objectId; __then(); }, true)); } else { __then(); } ; })(function __$__2() {

                return withProgress(util.format($("Updating application with objectId: '%s'."), objectId), function __1(log, _) { var __frame = { name: "__1", line: 149 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

                    return client.applications.patch(objectId, appParams, __cb(_, __frame, 1, 37, _, true)); }); }, __cb(_, __frame, 77, 22, function ___(__0, __5) { application = __5; __then(); }, true)); }); }); })(function ___(ex, __result) { __catch(function __$__2() { if (ex) { return (function __$__2(__then) {


                  if ((ex.statusCode && (ex.statusCode === 403))) {

                    return client.objects.getCurrentUser(__cb(_, __frame, 84, 49, function ___(__0, __3) { currentUserObject = __3;
                      if (((currentUserObject && currentUserObject.userType) && (currentUserObject.userType === "Guest"))) {
                        return _(new Error($("Updating an application is not allowed for a Guest user. Please contact your administrator to be added as a member in your tenant."))); } ; __then(); }, true)); } else { __then(); } ; })(function __$__2() {


                  return _(ex); }); } else { _(null, __result); } ; }, _); }); })(function ___() { __tryCatch(_, function __$__2() {


            cli.interaction.formatOutput(application, function(data) {
              if (data) {
                adUtils.displayAApplication(data, log); } ; }); _(); }); }); }, true)); }); });




  adApp.command("delete [objectId]").description($("Deletes the Active Directory application")).usage("[options] <object-id>").option("-o --objectId <objectId>", $("the object id of the application to remove")).option("-q, --quiet", $("quiet mode (do not ask for delete confirmation)")).execute(function __3(objectId, options, _) { var subscription, client, progress; var __frame = { name: "__3", line: 175 }; return __func(_, this, arguments, __3, 2, __frame, function __$__3() {





      if (!objectId) {
        return _(null, cli.missingArgument("objectId")); } ; return (function __$__3(_) {


        var __1 = !options.quiet; if (!__1) { return _(null, __1); } ; return cli.interaction.confirm(util.format($("Delete application %s? [y/n] "), objectId), __cb(_, __frame, 5, 45, function ___(__0, __3) { var __2 = !__3; return _(null, __2); }, true)); })(__cb(_, __frame, -174, 17, function ___(__0, __2) { return (function __$__3(__then) { if (__2) { return _(null); } else { __then(); } ; })(function __$__3() {


          subscription = profile.current.getSubscription(options.subscription);
          client = adUtils.getADGraphClient(subscription);
          progress = cli.interaction.progress(util.format($("Deleting application %s"), objectId)); return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__3() {

                return client.applications.deleteMethod(objectId, __cb(_, __frame, 12, 28, function __$__3() { _(null, null, true); }, true)); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__3() {

                  progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__3() { _(); }); }); }); }, true)); }); });



  adApp.command("list").description($("Get all Active Directory applications in current subscription's tenant")).execute(function __4(options, _) { var subscription, client, appParams, applications; var __frame = { name: "__4", line: 195 }; return __func(_, this, arguments, __4, 1, __frame, function __$__4() {



      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      appParams = { };

      return withProgress(util.format($("Listing applications")), function __1(log, _) { var __frame = { name: "__1", line: 202 }; return __func(_, this, arguments, __1, 1, __frame, function __$__1() {

          return client.applications.list(appParams, __cb(_, __frame, 1, 35, _, true)); }); }, __cb(_, __frame, 6, 25, function ___(__0, __2) { applications = __2;


        adUtils.displayApplications(applications, cli.interaction, log); _(); }, true)); }); });


  adApp.command("show").description($("Get Active Directory applications")).option("-a --applicationId <applicationId>", $("the name of the application to return")).option("-o --objectId <objectId>", $("the object id of the application to return")).option("-i --identifierUri <identifierUri>", $("the identifier uri of the application to return")).option("-c --search <search>", $("search display name of the application starting with the provided value")).execute(function __5(options, _) { var applicationId, objectId, identifierUri, search, subscription, client, progress, applications, parameters, app; var __frame = { name: "__5", line: 215 }; return __func(_, this, arguments, __5, 1, __frame, function __$__5() {






      applicationId = options.applicationId;
      objectId = options.objectId;
      identifierUri = options.identifierUri;
      search = options.search;

      adUtils.validateParameters({
        applicationId: applicationId,
        objectId: objectId,
        identifierUri: identifierUri,
        search: search });


      subscription = profile.current.getSubscription(options.subscription);
      client = adUtils.getADGraphClient(subscription);
      progress = cli.interaction.progress($("Getting Active Directory application(s)"));
      applications = [];
      parameters = null; return (function ___(__then) { (function ___(_) { __tryCatch(_, function __$__5() { return (function __$__5(__then) {

              if (applicationId) {
                parameters = { filter: (("appId eq '" + applicationId) + "'") };
                return client.applications.list(parameters, __cb(_, __frame, 21, 45, function ___(__0, __1) { applications = __1; __then(); }, true)); } else { return (function __$__5(__then) {
                  if (objectId) {
                    return client.applications.get(objectId, __cb(_, __frame, 23, 40, function ___(__0, __2) { app = __2;
                      if (app) {
                        applications.push(app); } ; __then(); }, true)); } else { return (function __$__5(__then) {

                      if (identifierUri) {
                        parameters = { filter: (("identifierUris/any(s:s eq '" + identifierUri) + "')") };
                        return client.applications.list(parameters, __cb(_, __frame, 29, 45, function ___(__0, __3) { applications = __3; __then(); }, true)); } else { return (function __$__5(__then) {
                          if (search) {
                            parameters = { filter: (("startswith(displayName,'" + search) + "')") };
                            return client.applications.list(parameters, __cb(_, __frame, 32, 45, function ___(__0, __4) { applications = __4; __then(); }, true)); } else { __then(); } ; })(__then); } ; })(__then); } ; })(__then); } ; })(function __$__5() { _(null, null, true); }); }); })(function ___(__e, __r, __cont) { (function ___(__then) { __tryCatch(_, function __$__5() {


              progress.end(); __then(); }); })(function ___() { __tryCatch(_, function ___() { if (__cont) { __then(); } else { _(__e, __r); }; }); }); }); })(function ___() { __tryCatch(_, function __$__5() {


          if ((applications.length > 0)) {
            adUtils.displayApplications(applications, cli.interaction, log); }
           else {
            log.data($("No matching application was found")); } ; _(); }); }); }); });};
