var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

import { selectDefinitionsFromManifest } from './Setting';

export var AppInfoService = function () {
  function AppInfoService() {
    _classCallCheck(this, AppInfoService);
  }

  _createClass(AppInfoService, [{
    key: 'determineAssetEndpoint',

    /**
     * @param api
     * @param appId
     * @param appVersion
     * @return {Promise.<string>}
     */
    value: function determineAssetEndpoint(_ref) {
      var api = _ref.api,
          appId = _ref.appId,
          appVersion = _ref.appVersion;

      return api.get('helpdesk/discover').then(function (_ref2) {
        var body = _ref2.body;
        return body.data.helpdesk_url;
      }).then(function (helpdeskUrl) {
        return helpdeskUrl.replace(/\/+$/, '') + '/file.php/apps/' + appId + '/v' + appVersion + '/files/assets';
      });
    }

    /**
     * @param api
     * @param instanceId
     * @return {Promise.<{}>}
     */

  }, {
    key: 'getInstanceInstallStatus',
    value: function getInstanceInstallStatus(_ref3) {
      var api = _ref3.api,
          instanceId = _ref3.instanceId;

      return api.get('apps/' + instanceId + '/status').then(function (_ref4) {
        var body = _ref4.body;

        return body;
      }).then(function (_ref5) {
        var is_installed = _ref5.is_installed;
        return is_installed;
      });
    }
  }, {
    key: 'loadManifestChanges',
    value: function loadManifestChanges(_ref6) {
      var api = _ref6.api,
          appId = _ref6.appId;

      return api.get('apps/packages/' + appId + '/manifest-changes').then(function (_ref7) {
        var body = _ref7.body;
        return body;
      });
    }

    /**
     * @return {Promise.<{}>}
     */

  }, {
    key: 'loadManifest',
    value: function loadManifest(_ref8) {
      var api = _ref8.api,
          appId = _ref8.appId;

      return api.get('apps/packages/' + appId + '/manifest').then(function (_ref9) {
        var body = _ref9.body;
        return body;
      });
    }

    /**
     * @param {{}} manifest
     * @param {Function} storage
     * @return {*}
     */

  }, {
    key: 'loadSettingValues',
    value: function loadSettingValues(manifest, storage) {
      var settings = selectDefinitionsFromManifest(manifest);
      if (settings.length) {
        var names = settings.map(function (setting) {
          return setting.name;
        });
        return storage.getAppStorage(names);
      }

      return Promise.resolve({});
    }
  }]);

  return AppInfoService;
}();