<commit_before>/**
* Copyright (c) Microsoft.  All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var util = require('util');

var Environment = require('./environment');
var Subscription = require('./subscription');
var publishSettings = require('./publishSettings');
var cacheUtils = require('../cacheUtils');
var log = require('../logging');
var StringSet = require('../set');
var utils = require('../utils');
var $ = utils.getLocaleString;

//
// Profile object - this manages the serialization of environment
// and subscription data for the current user.
//

function Profile() {
  var self = this;
  self.environments = {};
  Environment.publicEnvironments.forEach(function (env) {
    self.addEnvironment(env);
  });
  self.subscriptions = {};

  self.onSubscriptionUpdated = this.save.bind(this);
}

Object.defineProperty(Profile.prototype, 'currentSubscription', {
  enumerable: true,
  get: function () {
    return _.chain(this.subscriptions)
      .values()
      .filter(function (s) { return s.isDefault; })
      .first()
      .value() || null;
  },

  set: function (value) {
    _.values(this.subscriptions)
      .forEach(function (s) { s.isDefault = false; });
    if (value) {
      value.isDefault = true;
    }
  }
});

_.extend(Profile.prototype, {
  addEnvironment: function (env) {
    this.environments[env.name] = env;
  },

  getEnvironment: function (envName) {
    if (!envName) {
      return this.environments.AzureCloud;
    }
    var key = _.keys(this.environments)
      .filter(function (env) { return utils.ignoreCaseEquals(env, envName); })[0];
    return this.environments[key];
  },

  deleteEnvironment: function (environmentOrName) {
    if (_.isString(environmentOrName)) {
      delete this.environments[environmentOrName];
    } else {
      delete this.environments[environmentOrName.name];
    }
  },

  addSubscription: function (subscription) {
    var existingSubscription = _.values(this.subscriptions)
      .filter(function (s) { return s.id === subscription.id; })[0];

    if (existingSubscription) {
      existingSubscription.removeListener('updated', this.onSubscriptionUpdated);

      if (subscription.name !== existingSubscription.name) {
        delete this.subscriptions[existingSubscription.name];
      }

      _.extend(existingSubscription, subscription);
      subscription = existingSubscription;
    }

    if (subscription.isDefault) {
      this.currentSubscription = null;
    }
    this.subscriptions[subscription.name] = subscription;
    subscription.on('updated', this.onSubscriptionUpdated);
  },

  deleteSubscription: function (subscriptionOrName) {
    var subscription = subscriptionOrName;
    if (_.isString(subscriptionOrName)) {
      subscription = this.subscriptions[subscriptionOrName];
    }

    if (subscription.isDefault) {
      var remainingSubscriptions = _.values(this.subscriptions)
        .filter(function (sub) { return sub.name !== subscription.name; });
      if (_.first(remainingSubscriptions)) {
        remainingSubscriptions[0].isDefault = true;
      }
    }

    subscription.removeListener('updated', this.onSubscriptionUpdated);
    delete this.subscriptions[subscription.name];
  },

  getSubscription: function (idOrName) {
    if (!idOrName) {
      return this.currentSubscription;
    }
    return this.subscriptions[idOrName] ||
    _.values(this.subscriptions)
    .filter(function (s) { return utils.ignoreCaseEquals(s.id, idOrName); })[0];
  },

  importPublishSettings: function (fileName) {
    var self = this;
    _.each(publishSettings.import(fileName), function (subData) {
      var newSubscription = new Subscription(subData, self._findEnvironment(subData));
      self.addSubscription(newSubscription);<commit_msg>Added error check if no current subscription / subscription not found.<commit_after>/**
* Copyright (c) Microsoft.  All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var util = require('util');

var Environment = require('./environment');
var Subscription = require('./subscription');
var publishSettings = require('./publishSettings');
var cacheUtils = require('../cacheUtils');
var log = require('../logging');
var StringSet = require('../set');
var utils = require('../utils');
var $ = utils.getLocaleString;

//
// Profile object - this manages the serialization of environment
// and subscription data for the current user.
//

function Profile() {
  var self = this;
  self.environments = {};
  Environment.publicEnvironments.forEach(function (env) {
    self.addEnvironment(env);
  });
  self.subscriptions = {};

  self.onSubscriptionUpdated = this.save.bind(this);
}

Object.defineProperty(Profile.prototype, 'currentSubscription', {
  enumerable: true,
  get: function () {
    return _.chain(this.subscriptions)
      .values()
      .filter(function (s) { return s.isDefault; })
      .first()
      .value() || null;
  },

  set: function (value) {
    _.values(this.subscriptions)
      .forEach(function (s) { s.isDefault = false; });
    if (value) {
      value.isDefault = true;
    }
  }
});

_.extend(Profile.prototype, {
  addEnvironment: function (env) {
    this.environments[env.name] = env;
  },

  getEnvironment: function (envName) {
    if (!envName) {
      return this.environments.AzureCloud;
    }
    var key = _.keys(this.environments)
      .filter(function (env) { return utils.ignoreCaseEquals(env, envName); })[0];
    return this.environments[key];
  },

  deleteEnvironment: function (environmentOrName) {
    if (_.isString(environmentOrName)) {
      delete this.environments[environmentOrName];
    } else {
      delete this.environments[environmentOrName.name];
    }
  },

  addSubscription: function (subscription) {
    var existingSubscription = _.values(this.subscriptions)
      .filter(function (s) { return s.id === subscription.id; })[0];

    if (existingSubscription) {
      existingSubscription.removeListener('updated', this.onSubscriptionUpdated);

      if (subscription.name !== existingSubscription.name) {
        delete this.subscriptions[existingSubscription.name];
      }

      _.extend(existingSubscription, subscription);
      subscription = existingSubscription;
    }

    if (subscription.isDefault) {
      this.currentSubscription = null;
    }
    this.subscriptions[subscription.name] = subscription;
    subscription.on('updated', this.onSubscriptionUpdated);
  },

  deleteSubscription: function (subscriptionOrName) {
    var subscription = subscriptionOrName;
    if (_.isString(subscriptionOrName)) {
      subscription = this.subscriptions[subscriptionOrName];
    }

    if (subscription.isDefault) {
      var remainingSubscriptions = _.values(this.subscriptions)
        .filter(function (sub) { return sub.name !== subscription.name; });
      if (_.first(remainingSubscriptions)) {
        remainingSubscriptions[0].isDefault = true;
      }
    }

    subscription.removeListener('updated', this.onSubscriptionUpdated);
    delete this.subscriptions[subscription.name];
  },

  getSubscription: function (idOrName) {
    var subscription;
    if (!idOrName) {
      subscription = this.currentSubscription;
      if (!subscription) {
        throw new Error($('There is no current subscription. Please use the azure account login command to set your current subscription.'));
      }
    } else {
      subscription = this.subscriptions[idOrName] ||
      _.values(this.subscriptions)
      .filter(function (s) { return utils.ignoreCaseEquals(s.id, idOrName); })[0];
      if (!subscription) {
        throw new Error(util.format(
          $('The subscription \'%s\' was not found. Please check your spelling, or use the azure account login command to set your subscription.'),
          idOrName));
      }
    }
    return subscription;
  },

  importPublishSettings: function (fileName) {
    var self = this;
    _.each(publishSettings.import(fileName), function (subData) {
      var newSubscription = new Subscription(subData, self._findEnvironment(subData));
      self.addSubscription(newSubscription);
{'fraction_non_alphanumeric': 0.12394912696701875, 'fraction_numerical': 0.0017245095925846087, 'mean_word_length': 3.6604721245605223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7431739', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2572, 'n_words': 665}