"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 __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFilePaths = exports.untildify = void 0;
const os = require("node:os");
const node_url_1 = require("node:url");
const fastGlob = require("fast-glob");
// from https://github.com/sindresorhus/untildify/blob/f85a087418aeaa2beb56fe2684fe3b64fc8c588d/index.js#L11
function untildify(pathWithTilde) {
    const homeDirectory = os.homedir();
    return homeDirectory
        ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDirectory)
        : pathWithTilde;
}
exports.untildify = untildify;
function getFilePaths({ globPattern, rootPath, maxItems, }) {
    var _a, e_1, _b, _c;
    return __awaiter(this, void 0, void 0, function* () {
        if (rootPath.startsWith('file://')) {
            rootPath = (0, node_url_1.fileURLToPath)(rootPath);
        }
        const stream = fastGlob.stream([globPattern], {
            absolute: true,
            onlyFiles: true,
            cwd: rootPath,
            followSymbolicLinks: true,
            suppressErrors: true,
        });
        // NOTE: we use a stream here to not block the event loop
        // and ensure that we stop reading files if the glob returns
        // too many files.
        const files = [];
        let i = 0;
        try {
            for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a; _d = true) {
                _c = stream_1_1.value;
                _d = false;
                const fileEntry = _c;
                if (i >= maxItems) {
                    // NOTE: Close the stream to stop reading files paths.
                    stream.emit('close');
                    break;
                }
                files.push(fileEntry.toString());
                i++;
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (!_d && !_a && (_b = stream_1.return)) yield _b.call(stream_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return files;
    });
}
exports.getFilePaths = getFilePaths;
//# sourceMappingURL=fs.js.map