"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
var tslib_1 = require("tslib");
var error_1 = require("./error");
var mapagent_1 = require("./builders/mapagent");
var factory_1 = require("./builders/factory");
/**
 * The MapGuide HTTP client
 *
 * @export
 * @class Client
 * @implements {IMapGuideClient}
 */
var Client = /** @class */ (function () {
    function Client(agentUri, kind) {
        this.builder = (0, factory_1.createRequestBuilder)(agentUri, kind);
    }
    Client.prototype.getText = function (url) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var r, text;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, fetch(url)];
                    case 1:
                        r = _a.sent();
                        if (!r.ok)
                            throw new error_1.MgError(r.statusText);
                        return [4 /*yield*/, r.text()];
                    case 2:
                        text = _a.sent();
                        return [2 /*return*/, text];
                }
            });
        });
    };
    /**
     * Performs a generic GET request at the specified URL
     *
     * @template T The type of the object you are expecting to receive
     * @param {string} url The url to make the request to
     * @returns {Promise<T>} A promise for the value of the requested type
     *
     * @memberOf Client
     */
    Client.prototype.get = function (url) {
        return new Promise(function (resolve, reject) {
            fetch(url, {
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                method: "GET"
            })
                .then(function (response) {
                if ((0, mapagent_1.isErrorResponse)(response)) {
                    throw new error_1.MgError(response.statusText);
                }
                else {
                    resolve(response.json());
                }
            })
                .catch(reject);
        });
    };
    /**
     * Performs a generic POST request at the specified URL
     *
     * @template T The type of the object you are expecting to receive
     * @param {string} url The url to make the request to
     * @param {*} data The POST form data
     * @returns {Promise<T>} A promise for the value of the requested type
     *
     * @memberOf Client
     */
    Client.prototype.post = function (url, data) {
        if (!data.format) {
            data.format = "application/json";
        }
        //const form = new FormData();
        //for (const key in data) {
        //    form.append(key.toUpperCase(), data[key]);
        //}
        return new Promise(function (resolve, reject) {
            fetch(url, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                method: "POST",
                body: (0, mapagent_1.serialize)(data) //form
            })
                .then(function (response) {
                if ((0, mapagent_1.isErrorResponse)(response)) {
                    throw new error_1.MgError(response.statusText);
                }
                else {
                    resolve(response.json());
                }
            })
                .catch(reject);
        });
    };
    /**
     * Creates a new MapGuide session
     *
     * @param {string} username
     * @param {string} password
     * @returns {Promise<string>}
     *
     * @memberOf Client
     */
    Client.prototype.createSession = function (username, password) {
        return this.builder.createSession(username, password);
    };
    /**
     * Gets the server session timeout for the given session id
     *
     * @param {string} session
     * @returns {Promise<number>}
     *
     * @memberOf Client
     */
    Client.prototype.getServerSessionTimeout = function (session) {
        return this.builder.getServerSessionTimeout(session);
    };
    /**
     * gets the MapGuide Server version
     *
     * @since 0.14
     */
    Client.prototype.getSiteVersion = function () {
        return this.builder.getSiteVersion();
    };
    /**
     * Retrieves the requested resource
     *
     * @template T
     * @param {ResourceIdentifier} resourceId
     * @param {*} [args]
     * @returns {Promise<T>}
     *
     * @memberOf Client
     */
    Client.prototype.getResource = function (resourceId, args) {
        return this.builder.getResource(resourceId, args);
    };
    /**
     * Creates a runtime map from the specified map definition. Issues a v3.0.0 request
     *
     * @param {ICreateRuntimeMapOptions} options
     * @returns {Promise<RuntimeMap>}
     *
     * @memberOf Client
     */
    Client.prototype.createRuntimeMap = function (options) {
        return this.builder.createRuntimeMap(options);
    };
    /**
     * Creates a runtime map from the specified map definition. Issues a v4.0.0 request
     *
     * @param {ICreateRuntimeMapOptions} options
     * @returns {Promise<RuntimeMap>}
     *
     * @memberOf Client
     * @since 0.14.8
     */
    Client.prototype.createRuntimeMap_v4 = function (options) {
        return this.builder.createRuntimeMap_v4(options);
    };
    /**
     * Describes a runtime map. Issues a v3.0.0 request
     *
     * @param {IDescribeRuntimeMapOptions} options
     * @returns {Promise<RuntimeMap>}
     *
     * @memberOf Client
     */
    Client.prototype.describeRuntimeMap = function (options) {
        return this.builder.describeRuntimeMap(options);
    };
    /**
     * Describes a runtime map. Issues a v4.0.0 request
     *
     * @param {IDescribeRuntimeMapOptions} options
     * @returns {Promise<RuntimeMap>}
     *
     * @memberOf Client
     * @since 0.14.8
     */
    Client.prototype.describeRuntimeMap_v4 = function (options) {
        return this.builder.describeRuntimeMap_v4(options);
    };
    /**
     * Performs a map selection query on the current map
     *
     * @param {IQueryMapFeaturesOptions} options
     * @returns {Promise<QueryMapFeaturesResponse>}
     *
     * @memberOf Client
     */
    Client.prototype.queryMapFeatures = function (options) {
        return this.builder.queryMapFeatures(options);
    };
    /**
     * Performs a map selection query on the current map. Only applicable for use in MapGuide Open Source
     * 4.0 and higher
     *
     * @param {IQueryMapFeaturesOptions} options
     * @returns {Promise<QueryMapFeaturesResponse>}
     *
     * @memberOf Client
     */
    Client.prototype.queryMapFeatures_v4 = function (options) {
        return this.builder.queryMapFeatures_v4(options);
    };
    /**
     * Gets the tile template URL used by the viewer to send tile requests
     *
     * @param {string} resourceId
     * @param {string} groupName
     * @param {string} xPlaceholder
     * @param {string} yPlaceholder
     * @param {string} zPlaceholder
     * @returns {string}
     *
     * @memberOf Client
     * @since 0.14.8 added isXYZ parameter
     */
    Client.prototype.getTileTemplateUrl = function (resourceId, groupName, xPlaceholder, yPlaceholder, zPlaceholder, isXYZ) {
        return this.builder.getTileTemplateUrl(resourceId, groupName, xPlaceholder, yPlaceholder, zPlaceholder, isXYZ);
    };
    return Client;
}());
exports.Client = Client;
//# sourceMappingURL=client.js.map