import { pathNormalize } from "./Util/pathNormalize";
/**
 * Reference file resolver
 *
 * This class is used to resolve the file from the path
 *
 * It's responsibility is similar to a file system
 */
export class ReferenceFileResolver {
    /**
     * File list that can be resolved
     */
    files;
    _fileRootId;
    _fileMap = new Map();
    /**
     * Create a reference file resolver
     *
     * File root id can be root url, and becomes id for formats where texture is included in binary files, such as BPMX
     * @param files File list
     * @param rootUrl Root url for trim the path of the files
     * @param fileRootId File root id for give the unique id for the files
     */
    constructor(files, rootUrl, fileRootId) {
        rootUrl = pathNormalize(rootUrl);
        this.files = files;
        this._fileRootId = fileRootId;
        if (files.length === 0)
            return;
        if (files[0] instanceof File) {
            for (const file of files) {
                const fileRelativePath = pathNormalize(file.webkitRelativePath);
                if (!fileRelativePath.startsWith(rootUrl))
                    continue;
                const relativePath = fileRootId + pathNormalize(fileRelativePath.slice(rootUrl.length));
                this._fileMap.set(pathNormalize(relativePath).toUpperCase(), file);
            }
        }
        else {
            for (const file of files) {
                const relativePath = fileRootId + pathNormalize(file.relativePath);
                this._fileMap.set(pathNormalize(relativePath).toUpperCase(), file);
            }
        }
    }
    /**
     * Create full path from relative path for resolve the file
     * @param relativePath Relative path
     * @returns Full path
     */
    createFullPath(relativePath) {
        return this._fileRootId + pathNormalize(relativePath);
    }
    /**
     * Resolve the file from the path
     * @param path Path
     * @returns File
     */
    resolve(path) {
        const finalPath = pathNormalize(path);
        return this._fileMap.get(finalPath.toUpperCase());
    }
}
