"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
var jsonpointer_1 = __importDefault(require("jsonpointer"));
/**
 * Lookup a reference pointer within an OpenAPI definition and return the schema that it resolves
 * to.
 *
 * @param $ref Reference to look up a schema for.
 * @param definition OpenAPI definition to look for the `$ref` pointer in.
 */
function findSchemaDefinition($ref, definition) {
    if (definition === void 0) { definition = {}; }
    var origRef = $ref;
    $ref = $ref.trim();
    if ($ref === '') {
        // If this ref is empty, don't bother trying to look for it.
        return false;
    }
    if ($ref.startsWith('#')) {
        // Decode URI fragment representation.
        $ref = decodeURIComponent($ref.substring(1));
    }
    else {
        throw new Error("Could not find a definition for ".concat(origRef, "."));
    }
    var current = jsonpointer_1["default"].get(definition, $ref);
    if (current === undefined) {
        throw new Error("Could not find a definition for ".concat(origRef, "."));
    }
    return current;
}
exports["default"] = findSchemaDefinition;
