"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DidIonResolver = void 0;
const cross_fetch_1 = __importDefault(require("cross-fetch"));
// supports fetch in: node, browsers, and browser extensions.
// uses native fetch if available in environment or falls back to a ponyfill.
// 'cross-fetch' is a ponyfill that uses `XMLHTTPRequest` under the hood.
// `XMLHTTPRequest` cannot be used in browser extension background service workers.
// browser extensions get even more strict with `fetch` in that it cannot be referenced
// indirectly.
const fetch = (_a = globalThis.fetch) !== null && _a !== void 0 ? _a : cross_fetch_1.default;
/**
 * Resolver for ION DIDs.
 */
class DidIonResolver {
    /**
     * @param resolutionEndpoint optional custom URL to send DID resolution request to
     */
    constructor(resolutionEndpoint = 'https://discover.did.msidentity.com/1.0/identifiers/') {
        this.resolutionEndpoint = resolutionEndpoint;
    }
    method() {
        return 'ion';
    }
    resolve(did) {
        return __awaiter(this, void 0, void 0, function* () {
            // using `URL` constructor to handle both existence and absence of trailing slash '/' in resolution endpoint
            // appending './' to DID so 'did' in 'did:ion:abc' doesn't get interpreted as a URL scheme (e.g. like 'http') due to the colon
            const resolutionUrl = new URL('./' + did, this.resolutionEndpoint).toString();
            const response = yield fetch(resolutionUrl);
            if (response.status !== 200) {
                throw new Error(`unable to resolve ${did}, got http status ${response.status}`);
            }
            const didResolutionResult = yield response.json();
            return didResolutionResult;
        });
    }
    dump() {
        return __awaiter(this, void 0, void 0, function* () {
        });
    }
}
exports.DidIonResolver = DidIonResolver;
//# sourceMappingURL=did-ion-resolver.js.map