var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
/// <reference path="IRequestConfigOptions.ts" />
/// <reference path="../util/StringUtils.ts" />
/// <reference path="../base/BaseClass.ts" />
/// <reference path="context/Context.ts" />
/// <reference path="proxy/AjaxProxy.ts" />
var econda;
(function (econda) {
    var profileaccess;
    (function (profileaccess) {
        var Context = econda.profileaccess.context.Context;
        /**
         * Baseclass for profileaccess endpoint requests.
         *
         * Should not be used directly.
         *
         * @class econda.profileaccess.Request
         * @extends econda.base.BaseClass
         */
        var Request = (function (_super) {
            __extends(Request, _super);
            function Request(cfg) {
                if (cfg === void 0) { cfg = null; }
                _super.call(this);
                /**
                 * Endpoint key as defined in profile access management interface
                 * @cfg {String} endpointKey
                 * @accessor
                 */
                this.endpointKey = null;
                /**
                 * Account id (client key). This could be also a crosssell account id.
                 *
                 * Optional if econdaConfig.crosssellAccountId is set.
                 *
                 * @cfg {String} accountId
                 */
                this.accountId = null;
                /**
                 * Context of request.
                 * If defaultContextEnabled is set to true, this function will return this default context on first call.
                 * If set to false, it will return null.
                 *
                 * @cfg {econda.profileaccess.context.Context} context
                 * @accessor
                 */
                this.context = null;
                /**
                 * True to enable a default context for all requests.
                 * @cfg {boolean} [defaultContextEnabled=true]
                 * @accessor
                 */
                this.defaultContextEnabled = true;
                /**
                 * Proxy to use for request
                 * @cfg {String} proxy name or instance of IProxy interface
                 * @accessor
                 */
                this.proxy = null;
                /**
                 * Callback handle after successful request.
                 * @cfg {Function} success
                 * @accessor
                 */
                this.success = null;
                /**
                 * Function to call if there was an error during cross Sell ajax request.
                 * @cfg {Function} error
                 * @accessor
                 */
                this._error = null;
                /**
                 * Function to call after request has finished (successful or not)
                 * @cfg {Function} callback
                 * @accessor
                 */
                this._callback = null;
                /**
                 * True if last request was successfull.
                 * @property {Boolean} requestWasSuccessful
                 */
                this.requestWasSuccessful = null;
                if (cfg instanceof Request) {
                    return cfg;
                }
                else {
                    this.initConfig(cfg);
                }
            }
            Request.prototype.getEndpointKey = function () {
                return this.endpointKey;
            };
            Request.prototype.setEndpointKey = function (endpointKey) {
                this.endpointKey = endpointKey;
                return this;
            };
            /**
             * Returns account id or account id of global configuration (econdaConfig.crosssellAccountId) or null if not set.
             *
             * The service can process a client key or a crosssell accountid.
             *
             * @returns String
             */
            Request.prototype.getAccountId = function () {
                return this.accountId || econdaConfig.crosssellAccountId;
            };
            /**
             * Set account id for this request. Setting Cross Sell accountId on global config
             * (econdaConfig.crosssellAccountId) might be a better place.
             * @param {String} accountId
             * @chainable
             */
            Request.prototype.setAccountId = function (accountId) {
                this.accountId = accountId;
                return this;
            };
            Request.prototype.getContext = function () {
                return this.defaultContextEnabled
                    ? this.context || new Context()
                    : this.context;
            };
            Request.prototype.setContext = function (context) {
                this.context = new Context(context);
                return this;
            };
            Request.prototype.getDefaultContextEnabled = function () {
                return this.defaultContextEnabled;
            };
            Request.prototype.setDefaultContextEnabled = function (enabled) {
                this.defaultContextEnabled = (enabled === true);
            };
            Request.prototype.getProxy = function () {
                return this.proxy;
            };
            Request.prototype.setProxy = function (proxy) {
                if (typeof proxy === 'string') {
                    var className = econda.util.StringUtils.ucFirst(proxy);
                    if (typeof econda.profileaccess.proxy[className] !== 'undefined') {
                        this.proxy = new econda.profileaccess.proxy[className]();
                    }
                    else {
                        throw 'proxy not supported: ' + proxy;
                    }
                }
                else {
                    this.proxy = proxy;
                }
                if (this.proxy && typeof this.proxy.setRequest !== 'undefined') {
                    this.proxy.setRequest(this);
                }
                return this;
            };
            Request.prototype.getSuccess = function () {
                return this.success;
            };
            Request.prototype.setSuccess = function (callback) {
                this.success = callback;
                return this;
            };
            Request.prototype.getError = function () {
                return this._error;
            };
            Request.prototype.setError = function (callback) {
                this._error = callback;
                return this;
            };
            Request.prototype.getCallback = function () {
                return this._callback;
            };
            Request.prototype.setCallback = function (callback) {
                this._callback = callback;
                return this;
            };
            /**
             * Send request. Set callbacks to handle result.
             */
            Request.prototype.send = function () {
                var proxy = this.proxy;
                if (!proxy) {
                    proxy = new econda.profileaccess.proxy.AjaxProxy();
                    proxy.setRequest(this);
                }
                this._validateAndLogErrors();
                proxy.send();
            };
            /**
             * Log errors to debug
             * @protected
             */
            Request.prototype._validateAndLogErrors = function () {
                if (!this.getAccountId()) {
                    econda.debug.error('Missing crosssell account id in request.', this);
                }
                if (!this.getEndpointKey()) {
                    econda.debug.error('Missing endpoint key in request', this);
                }
            };
            /**
             * Handles response from proxy, must be called from proxy class
             * @param {Object} response
             */
            Request.prototype.handleResponse = function (response) {
                if (response.isError) {
                    if (typeof this._error === 'function') {
                        this._error(response);
                    }
                }
                else {
                    if (typeof this.success === 'function') {
                        this.success(response);
                    }
                }
                if (typeof this._callback === 'function') {
                    this._callback(response);
                }
            };
            return Request;
        }(econda.base.BaseClass));
        profileaccess.Request = Request; // end of class
    })(profileaccess = econda.profileaccess || (econda.profileaccess = {}));
})(econda || (econda = {})); // end of module