"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var Link_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Link = void 0;
const ta_json_1 = require("ta-json");
const urijs_1 = __importDefault(require("urijs"));
const guard_1 = __importDefault(require("./guard"));
let Link = Link_1 = class Link {
    constructor(uri, title, templated) {
        this.templated = false;
        if (typeof uri === "string") {
            this.href = uri;
        }
        else {
            this.href = uri.href();
        }
        if (title) {
            this.title = title;
        }
        this.templated = !!templated;
    }
    getVariableValue(template, variable) {
        guard_1.default.notNullOrUndefined(template);
        guard_1.default.stringNotNullOrEmpty(variable);
        guard_1.default.stringNotNullOrEmpty(this.href);
        const templateUri = new urijs_1.default(template.href);
        const currentUri = new urijs_1.default(this.href);
        const index = templateUri
            .path()
            .split("/")
            .findIndex(item => item.startsWith(`{${variable}}`));
        if (index) {
            return decodeURIComponent(currentUri.path().split("/")[index]);
        }
        let queryRegexStr = `\?|,${variable.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")},|$`;
        let queryRegex = new RegExp(queryRegexStr);
        if (queryRegex.test(templateUri.query())) {
            queryRegexStr = `(?:^|&)${variable.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")}=([^#&]+)(?:[#&]?|$)`;
            queryRegex = new RegExp(queryRegexStr);
            const matches = queryRegex.exec(currentUri.query());
            if (matches) {
                return decodeURIComponent(matches[1]);
            }
        }
        throw `Variable '${variable}' not found in template '${template.href}'.`;
    }
    static copyFrom(source) {
        return new Link_1(source.href, source.title, source.templated);
    }
};
exports.Link = Link;
__decorate([
    (0, ta_json_1.JsonProperty)("href"),
    __metadata("design:type", String)
], Link.prototype, "href", void 0);
__decorate([
    (0, ta_json_1.JsonProperty)("title"),
    __metadata("design:type", String)
], Link.prototype, "title", void 0);
__decorate([
    (0, ta_json_1.JsonProperty)("templated"),
    __metadata("design:type", Boolean)
], Link.prototype, "templated", void 0);
exports.Link = Link = Link_1 = __decorate([
    (0, ta_json_1.JsonObject)(),
    __metadata("design:paramtypes", [Object, String, Boolean])
], Link);
exports.default = Link;
//# sourceMappingURL=link.js.map