"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SymlinkAnalyzer = void 0;
const path = __importStar(require("node:path"));
const node_core_library_1 = require("@rushstack/node-core-library");
class SymlinkAnalyzer {
    constructor(options = {}) {
        // The directory tree discovered so far
        this._nodesByPath = new Map();
        // The symlinks that we encountered while building the directory tree
        this._linkInfosByPath = new Map();
        this._requiredSourceParentPath = options.requiredSourceParentPath;
    }
    async analyzePathAsync(options) {
        const { inputPath, preserveLinks = false, shouldIgnoreExternalLink } = options;
        // First, try to short-circuit the analysis if we've already analyzed this path
        const resolvedPath = path.resolve(inputPath);
        const existingNode = this._nodesByPath.get(resolvedPath);
        if (existingNode) {
            return existingNode;
        }
        // Postfix a '/' to the end of the path. This will get trimmed off later, but it
        // ensures that the last path component is included in the loop below.
        let targetPath = `${resolvedPath}${path.sep}`;
        let targetPathIndex = -1;
        let currentNode;
        while ((targetPathIndex = targetPath.indexOf(path.sep, targetPathIndex + 1)) >= 0) {
            if (targetPathIndex === 0) {
                // Edge case for a Unix path like "/folder/file" --> [ "", "folder", "file" ]
                continue;
            }
            const currentPath = targetPath.slice(0, targetPathIndex);
            currentNode = this._nodesByPath.get(currentPath);
            if (currentNode === undefined) {
                const linkStats = await node_core_library_1.FileSystem.getLinkStatisticsAsync(currentPath);
                if (linkStats.isSymbolicLink()) {
                    // Link target paths can be relative or absolute, so we need to resolve them
                    const linkTargetPath = await node_core_library_1.FileSystem.readLinkAsync(currentPath);
                    const resolvedLinkTargetPath = path.resolve(path.dirname(currentPath), linkTargetPath);
                    // Do a check to make sure that the link target path is not outside the source folder
                    if (this._requiredSourceParentPath) {
                        const relativeLinkTargetPath = path.relative(this._requiredSourceParentPath, resolvedLinkTargetPath);
                        if (relativeLinkTargetPath.startsWith('..')) {
                            // Symlinks that link outside of the source folder may be ignored. Check to see if we
                            // can ignore this one and if so, return undefined.
                            if (shouldIgnoreExternalLink === null || shouldIgnoreExternalLink === void 0 ? void 0 : shouldIgnoreExternalLink(currentPath)) {
                                return undefined;
                            }
                            throw new Error(`Symlink targets not under folder "${this._requiredSourceParentPath}": ` +
                                `${currentPath} -> ${resolvedLinkTargetPath}`);
                        }
                    }
                    currentNode = {
                        kind: 'link',
                        nodePath: currentPath,
                        linkStats,
                        linkTarget: resolvedLinkTargetPath
                    };
                }
                else if (linkStats.isDirectory()) {
                    currentNode = {
                        kind: 'folder',
                        nodePath: currentPath,
                        linkStats
                    };
                }
                else if (linkStats.isFile()) {
                    currentNode = {
                        kind: 'file',
                        nodePath: currentPath,
                        linkStats
                    };
                }
                else {
                    throw new Error('Unknown object type: ' + currentPath);
                }
                this._nodesByPath.set(currentPath, currentNode);
            }
            if (!preserveLinks) {
                while ((currentNode === null || currentNode === void 0 ? void 0 : currentNode.kind) === 'link') {
                    const targetNode = await this.analyzePathAsync({
                        inputPath: currentNode.linkTarget,
                        preserveLinks: true
                    });
                    // Have we created an ILinkInfo for this link yet?
                    if (!this._linkInfosByPath.has(currentNode.nodePath)) {
                        // Follow any symbolic links to determine whether the final target is a directory
                        const targetStats = await node_core_library_1.FileSystem.getStatisticsAsync(targetNode.nodePath);
                        const targetIsDirectory = targetStats.isDirectory();
                        const linkInfo = {
                            kind: targetIsDirectory ? 'folderLink' : 'fileLink',
                            linkPath: currentNode.nodePath,
                            targetPath: targetNode.nodePath
                        };
                        this._linkInfosByPath.set(currentNode.nodePath, linkInfo);
                    }
                    const nodeTargetPath = targetNode.nodePath;
                    const remainingPath = targetPath.slice(targetPathIndex);
                    targetPath = path.join(nodeTargetPath, remainingPath);
                    targetPathIndex = nodeTargetPath.length;
                    currentNode = targetNode;
                }
            }
            if (targetPath.length === targetPathIndex + 1) {
                // We've reached the end of the path
                break;
            }
        }
        if (!currentNode) {
            throw new Error('Unable to analyze path: ' + inputPath);
        }
        return currentNode;
    }
    /**
     * Returns a summary of all the symbolic links encountered by {@link SymlinkAnalyzer.analyzePathAsync}.
     */
    reportSymlinks() {
        const list = [...this._linkInfosByPath.values()];
        node_core_library_1.Sort.sortBy(list, (x) => x.linkPath);
        return list;
    }
}
exports.SymlinkAnalyzer = SymlinkAnalyzer;
//# sourceMappingURL=SymlinkAnalyzer.js.map