"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureProjection = exports.resolveProjectionFromEpsgIoAsync = void 0;
var tslib_1 = require("tslib");
var proj4_1 = tslib_1.__importDefault(require("proj4"));
var error_1 = require("../error");
var i18n_1 = require("../i18n");
var string_1 = require("../../utils/string");
var proj4_2 = require("ol/proj/proj4");
var logger_1 = require("../../utils/logger");
/**
 * Performs a projection definititon lookup at epsg.io for the given EPSG code
 *
 * @export
 * @param {string | number} epsg
 * @param {string} locale
 * @param {string} mapDef
 * @returns {Promise<any>}
 * @since 0.13
 */
function resolveProjectionFromEpsgIoAsync(epsg, locale, mapDef) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var r, resp;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4 /*yield*/, fetch("https://epsg.io?format=json&q=".concat(epsg))];
                case 1:
                    r = _a.sent();
                    return [4 /*yield*/, r.json()];
                case 2:
                    resp = _a.sent();
                    if (resp.results && resp.results.length > 0) {
                        proj4_1.default.defs("EPSG:".concat(epsg), resp.results[0].proj4);
                        (0, logger_1.debug)("Registered projection EPSG:".concat(epsg, " from epsg.io"));
                        return [2 /*return*/, proj4_1.default.defs["EPSG:".concat(epsg)]];
                    }
                    else {
                        throw new error_1.MgError((0, i18n_1.tr)("INIT_ERROR_UNREGISTERED_EPSG_CODE", locale, { epsg: epsg, mapDefinition: mapDef }));
                    }
                    return [2 /*return*/];
            }
        });
    });
}
exports.resolveProjectionFromEpsgIoAsync = resolveProjectionFromEpsgIoAsync;
/**
 * Ensures the given projection (by EPSG code) exists and if not invokes the given factory function (or does an epsg.io lookup)
 * to fetch the definition to be registered.
 *
 * Once registered, it will update the projection set within OpenLayers
 *
 * @export
 * @param {number} epsgCode
 * @param {() => Promise<string>} [factoryIfNotFound] A custom factory function to provide the required proj4js string for this projection. If not specified, a lookup to epsg.io will be done instead
 * @param {string} alias
 * @returns {Promise<string>}
 * @since 0.13
 */
function ensureProjection(epsgCode, locale, alias, factoryIfNotFound) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var resolvedName, bAdded, name;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    bAdded = false;
                    name = "EPSG:".concat(epsgCode);
                    if (!proj4_1.default.defs[name]) return [3 /*break*/, 1];
                    resolvedName = name;
                    return [3 /*break*/, 4];
                case 1:
                    if (!factoryIfNotFound) return [3 /*break*/, 2];
                    proj4_1.default.defs[name] = factoryIfNotFound();
                    bAdded = true;
                    resolvedName = name;
                    return [3 /*break*/, 4];
                case 2: return [4 /*yield*/, resolveProjectionFromEpsgIoAsync(epsgCode, locale, "")];
                case 3:
                    _a.sent();
                    resolvedName = name;
                    _a.label = 4;
                case 4:
                    //Register the alias if specified
                    if (!(0, string_1.strIsNullOrEmpty)(alias) && !proj4_1.default.defs[alias]) {
                        proj4_1.default.defs[alias] = proj4_1.default.defs[resolvedName];
                        bAdded = true;
                    }
                    //Need to call re-register if proj4.defs got changed so OL can pick it up
                    if (bAdded) {
                        (0, proj4_2.register)(proj4_1.default);
                    }
                    return [2 /*return*/, [epsgCode, resolvedName]];
            }
        });
    });
}
exports.ensureProjection = ensureProjection;
//# sourceMappingURL=projections.js.map