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="IProxy.ts" />
/// <reference path="../../util/Json.ts" />
/// <reference path="../../util/DateUtils.ts" />
/// <reference path="../../util/ZeroGapUtil.ts" />
/// <reference path="../../ajax/Ajax.ts" />
/// <reference path="../Request.ts" />
/// <reference path="../../net/Uri.ts" />
var econda;
(function (econda) {
    var profileaccess;
    (function (profileaccess) {
        var proxy;
        (function (proxy) {
            var ZeroGapUtil = econda.util.ZeroGapUtil;
            /**
             * Proxy to send requests to profile access service.
             * This is an internal class an there's no reason to use this class directly.
             * @class econda.profileaccess.proxy.AjaxProxy
             */
            var AjaxProxy = (function (_super) {
                __extends(AjaxProxy, _super);
                function AjaxProxy(cfg) {
                    if (cfg === void 0) { cfg = null; }
                    _super.call(this);
                    /**
                     * Complete Service uri. Used for unit tests only
                     * @cfg {String} uri
                     * @accessor
                     */
                    this.uri = null;
                    /**
                     * Service part of uri. Used for unit tests only
                     * @cfg {String} serviceUri
                     * @accessor
                     */
                    this.serviceUri = null;
                    /**
                     * Reference to request object
                     * @cfg {econda.profileaccess.Request} request
                     * @accessor
                     */
                    this.request = null;
                    /**
                     * Last ajax request object. We use this for unit testing.
                     * @private
                     * @property {econda.ajax.Request}
                     */
                    this._ajaxRequest = null;
                    this._isPost = false;
                    this._hasContextParams = false;
                    if (cfg instanceof AjaxProxy) {
                        return cfg;
                    }
                    this.initConfig(cfg);
                }
                AjaxProxy.prototype.getUri = function () {
                    return this.uri;
                };
                AjaxProxy.prototype.setUri = function (uri) {
                    this.uri = uri;
                };
                AjaxProxy.prototype.getServiceUri = function () {
                    return this.serviceUri;
                };
                AjaxProxy.prototype.setServiceUri = function (serviceUri) {
                    this.serviceUri = serviceUri;
                };
                AjaxProxy.prototype.setRequest = function (request) {
                    this.request = request;
                };
                AjaxProxy.prototype.getRequest = function () {
                    return this.request;
                };
                /**
                 * Get instance of the ajax request class that was used to perform the request.
                 * @returns {econda.ajax.Request}
                 */
                AjaxProxy.prototype.getAjaxRequest = function () {
                    return this._ajaxRequest;
                };
                /**
                 * Send request and call {@link #handleSuccess} when done.
                 */
                AjaxProxy.prototype.send = function () {
                    var cmp = this;
                    var uri = this.uri;
                    if (!uri) {
                        uri = this.getEndpointUri();
                    }
                    var params = {};
                    var headerValues = this.getHeaderValues();
                    var context = this.request.getContext();
                    if (context && this._hasContextParams) {
                        context.addToRequest(params);
                        // append history from profile
                        ZeroGapUtil.appendHistoryFromProfile(params);
                    }
                    if (this._isPost) {
                        var request = this._ajaxRequest = econda.ajax.Ajax.createRequest({
                            uri: uri,
                            method: 'post',
                            writer: 'form-encoded',
                            reader: 'json',
                            data: params,
                            headers: headerValues,
                            success: function (ajaxResponse) {
                                cmp.handleSuccess(ajaxResponse);
                            },
                            error: function (error) {
                                cmp.handleError(error);
                            }
                        });
                        request.send();
                    }
                    else {
                        var request = this._ajaxRequest = econda.ajax.Ajax.createRequest({
                            uri: uri,
                            method: 'get',
                            reader: 'json',
                            params: params,
                            headers: headerValues,
                            success: function (ajaxResponse) {
                                cmp.handleSuccess(ajaxResponse);
                            },
                            error: function (error) {
                                cmp.handleError(error);
                            }
                        });
                        request.send();
                    }
                };
                AjaxProxy.prototype.setIsPost = function (value) {
                    this._isPost = value;
                };
                AjaxProxy.prototype.setHasContextParams = function (value) {
                    this._hasContextParams = value;
                };
                AjaxProxy.prototype.getEndpointUri = function () {
                    var uri = this.uri;
                    var req = this.request;
                    if (!uri) {
                        var protocol = econda.net.Uri.detectProtocol();
                        var serviceUri = this.getServiceUri();
                        if (!serviceUri) {
                            serviceUri = 'services.crosssell.info/profileaccess';
                        }
                        uri = [protocol, '://', serviceUri, '/', req.getAccountId(), '/profiles/', req.getEndpointKey()].join('');
                    }
                    return uri;
                };
                AjaxProxy.prototype.handleError = function (error) {
                    if (!error || !error.isError) {
                        error = {
                            isError: true
                        };
                    }
                    this.request.handleResponse(error);
                };
                /**
                 * Handles ajax response data, creates Response object and calls callback on Request object
                 * @private
                 */
                AjaxProxy.prototype.handleSuccess = function (responseData) {
                    if (!responseData) {
                        this.handleError(null);
                        return;
                    }
                    this.request.handleResponse(responseData);
                };
                AjaxProxy.prototype.getHeaderValues = function () {
                    var header = {};
                    return header;
                };
                return AjaxProxy;
            }(econda.base.BaseClass));
            proxy.AjaxProxy = AjaxProxy;
        })(proxy = profileaccess.proxy || (profileaccess.proxy = {}));
    })(profileaccess = econda.profileaccess || (econda.profileaccess = {}));
})(econda || (econda = {}));