"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(_a) {
    return __awaiter(this, arguments, void 0, function* ({ globPattern, rootPath, maxItems, }) {
        var _b, e_1, _c, _d;
        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 _e = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _b = stream_1_1.done, !_b; _e = true) {
                _d = stream_1_1.value;
                _e = false;
                const fileEntry = _d;
                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 (!_e && !_b && (_c = stream_1.return)) yield _c.call(stream_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return files;
    });
}
exports.getFilePaths = getFilePaths;
//# sourceMappingURL=fs.js.map