/*!
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */
import { getDriveItemByRootFileName, } from "@fluidframework/odsp-doclib-utils";
import { OdspDriverUrlResolver, createOdspUrl } from "@fluidframework/odsp-driver";
export class OdspUrlResolver {
    constructor(server, authRequestInfo) {
        this.server = server;
        this.authRequestInfo = authRequestInfo;
        this.driverUrlResolver = new OdspDriverUrlResolver();
    }
    async resolve(request) {
        try {
            const resolvedUrl = await this.driverUrlResolver.resolve(request);
            return resolvedUrl;
        }
        catch (error) { }
        const url = new URL(request.url);
        const fullPath = url.pathname.substr(1);
        const documentId = fullPath.split("/")[0];
        const documentRelativePath = fullPath.slice(documentId.length + 1);
        const filePath = this.formFilePath(documentId);
        const { drive, item } = await getDriveItemByRootFileName(this.server, "", filePath, this.authRequestInfo, true);
        const odspUrl = createOdspUrl(`https://${this.server}`, drive, item, documentRelativePath);
        return this.driverUrlResolver.resolve({ url: odspUrl, headers: request.headers });
    }
    formFilePath(documentId) {
        const encoded = encodeURIComponent(`${documentId}.fluid`);
        return `/r11s/${encoded}`;
    }
    async getAbsoluteUrl(resolvedUrl, relativeUrl) {
        return this.driverUrlResolver.getAbsoluteUrl(resolvedUrl, relativeUrl);
    }
    async createCreateNewRequest(fileName) {
        const filePath = "/r11s/";
        const driveItem = await getDriveItemByRootFileName(this.server, "", filePath, this.authRequestInfo, false);
        return this.driverUrlResolver.createCreateNewRequest(`https://${this.server}`, driveItem.drive, filePath, `${fileName}.fluid`);
    }
}
//# sourceMappingURL=odspUrlResolver.js.map