import{Pb as T,Qb as z}from"./chunk-m07cv1kh.js";import"./chunk-rjpqpn2z.js";import G from"node:fs";import I from"node:path";function C(j,L){let K=T(j);if(K.length===0)return L;if(z(j))return K;return[...new Set([...L,...K])]}function O(j){let L=[],K=G.readdirSync(j,{withFileTypes:!0});for(let H of K){let Q=I.join(j,H.name);if(H.isDirectory())L.push(...O(Q));else if(H.isFile()&&H.name.endsWith(".ts")&&H.name!=="index.ts"&&H.name!=="aliases.ts")L.push(Q)}return L}function V(j,L){let K=j.split("."),H=L.split("."),Q=Math.max(K.length,H.length);for(let W=0;W<Q;W++){let q=K[W],J=H[W];if(q===void 0)return-1;if(J===void 0)return 1;let X=/^\d+$/.test(q),Y=/^\d+$/.test(J);if(X&&Y){let $=Number.parseInt(q,10)-Number.parseInt(J,10);if($!==0)return $}else if(X!==Y)return X?-1:1;else if(q!==J)return q<J?-1:1}return 0}function N(j,L){let K=(j.startsWith("v")?j.slice(1):j).split("+")[0],H=(L.startsWith("v")?L.slice(1):L).split("+")[0],Q=K.indexOf("-"),W=Q===-1?K:K.slice(0,Q),q=Q===-1?null:K.slice(Q+1),J=H.indexOf("-"),X=J===-1?H:H.slice(0,J),Y=J===-1?null:H.slice(J+1),$=W.split("."),U=X.split(".");for(let w=0;w<Math.max($.length,U.length);w++){let E=Number.parseInt($[w]||"0",10),R=Number.parseInt(U[w]||"0",10);if(E>R)return-1;if(E<R)return 1}if(q===null&&Y!==null)return-1;if(q!==null&&Y===null)return 1;if(q!==null&&Y!==null){let w=V(q,Y);return w===0?0:-w}return 0}function _(j){return j.replace(/\\/g,"\\\\").replace(/"/g,"\\\"").replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}function x(j){let L=j.programs.map((q)=>`"${_(q)}"`).join(", "),K=j.dependencies.map((q)=>`"${_(q)}"`).join(", "),H=j.buildDependencies.map((q)=>`"${_(q)}"`).join(", "),Q=j.aliases.map((q)=>`"${_(q)}"`).join(", "),W=j.versions.map((q)=>`"${_(q)}"`).join(", ");return`    .{
        .name = "${_(j.name)}",
        .domain = "${_(j.domain)}",
        .description = "${_(j.description)}",
        .homepage_url = ${j.homepageUrl?`"${_(j.homepageUrl)}"`:"null"},
        .programs = &[_][]const u8{ ${L} },
        .dependencies = &[_][]const u8{ ${K} },
        .build_dependencies = &[_][]const u8{ ${H} },
        .aliases = &[_][]const u8{ ${Q} },
        .versions = &[_][]const u8{ ${W} },
    }`}async function S(j,L){console.log(`\uD83D\uDD0D Scanning packages directory: ${j}`);let K=O(j);console.log(`\uD83D\uDCE6 Found ${K.length} package files`);let H=[],Q=0,W=0;for(let J of K)try{let Y=G.readFileSync(J,"utf-8").match(/export const (\w+) = \{/);if(!Y){console.warn(`⚠️  Could not find export const in ${J}`),W++;continue}let w=(await import(`file://${I.resolve(J)}`))[Y[1]];if(!w||!w.domain){console.warn(`⚠️  Invalid package data in ${J}`),W++;continue}let E=Array.isArray(w.versions)?[...w.versions].sort(N):[];if(H.push({name:w.name||w.domain,domain:w.domain,description:w.description||"",homepageUrl:w.homepageUrl,programs:Array.isArray(w.programs)?w.programs:[],dependencies:Array.isArray(w.dependencies)?w.dependencies:[],buildDependencies:Array.isArray(w.buildDependencies)?w.buildDependencies:[],aliases:C(w.domain,Array.isArray(w.aliases)?w.aliases:[]),versions:E}),Q++,Q%100===0)console.log(`  Processed ${Q}/${K.length} packages...`)}catch(X){console.error(`❌ Error processing ${J}:`,X),W++}console.log(`✅ Processed ${Q} packages (${W} errors)`),H.sort((J,X)=>J.domain.localeCompare(X.domain));let q=M(H);G.writeFileSync(L,q,"utf-8"),console.log(`✅ Generated Zig definitions: ${L}`),console.log(`\uD83D\uDCCA Total packages: ${H.length}`)}function M(j){let L=j.map((K)=>x(K)).join(`,
`);return`//! Package definitions generated from ts-pantry
//! DO NOT EDIT MANUALLY - This file is auto-generated
//! Generated on: ${new Date().toISOString()}
//! Total packages: ${j.length}

const std = @import("std");

/// Package information
pub const PackageInfo = struct {
    name: []const u8,
    domain: []const u8,
    description: []const u8,
    homepage_url: ?[]const u8,
    programs: []const []const u8,
    dependencies: []const []const u8,
    build_dependencies: []const []const u8,
    aliases: []const []const u8,
    versions: []const []const u8,
};

/// All known packages (${j.length} total)
pub const packages = [_]PackageInfo{
${L}
};

/// Get package by domain name
pub fn getPackageByDomain(domain: []const u8) ?*const PackageInfo {
    for (&packages) |*pkg| {
        if (std.mem.eql(u8, pkg.domain, domain)) {
            return pkg;
        }
    }
    return null;
}

/// Get package by name (checks domain and aliases)
pub fn getPackageByName(name: []const u8) ?*const PackageInfo {
    for (&packages) |*pkg| {
        // Check domain
        if (std.mem.eql(u8, pkg.domain, name)) {
            return pkg;
        }

        // Check full name
        if (std.mem.eql(u8, pkg.name, name)) {
            return pkg;
        }

        // Check aliases
        for (pkg.aliases) |alias| {
            if (std.mem.eql(u8, alias, name)) {
                return pkg;
            }
        }
    }
    return null;
}

/// Get all package domains
pub fn getAllDomains(allocator: std.mem.Allocator) ![][]const u8 {
    // eslint-disable-next-line prefer-const
    var domains = try allocator.alloc([]const u8, packages.len);
    for (packages, 0..) |pkg, i| {
        domains[i] = pkg.domain;
    }
    return domains;
}

/// Get total package count
pub fn getPackageCount() usize {
    return packages.len;
}

test "Package registry has entries" {
    try std.testing.expect(packages.len > 0);
}

test "Can find node package" {
    const node = getPackageByName("node");
    if (node) |pkg| {
        try std.testing.expectEqualStrings("nodejs.org", pkg.domain);
    }
}

test "Can find package by domain" {
    const node = getPackageByDomain("nodejs.org");
    try std.testing.expect(node != null);
}

test "Can find package by alias" {
    const bun = getPackageByName("bun");
    if (bun) |pkg| {
        try std.testing.expect(
            std.mem.eql(u8, pkg.domain, "bun.sh") or
            std.mem.eql(u8, pkg.domain, "bun.com")
        );
    }
}
`}async function P(j,L){console.log(`\uD83D\uDD0D Generating Zig aliases from: ${j}`);let K=O(j),H={};for(let q of K)try{let X=G.readFileSync(q,"utf-8").match(/export const (\w+) = \{/);if(!X)continue;let U=(await import(`file://${I.resolve(q)}`))[X[1]];if(!U||!U.domain)continue;if(U.aliases&&Array.isArray(U.aliases))for(let E of U.aliases)H[E]=U.domain;if(U.name&&U.name!==U.domain)H[U.name]=U.domain;let w=U.domain.split(".")[0];if(w&&!H[w])H[w]=U.domain}catch(J){console.error(`Error processing ${q}:`,J)}let Q=Object.entries(H).sort(([q],[J])=>q.localeCompare(J)).map(([q,J])=>`    .{ .alias = "${_(q)}", .domain = "${_(J)}" }`).join(`,
`),W=`//! Package aliases generated from ts-pantry
//! DO NOT EDIT MANUALLY - This file is auto-generated
//! Generated on: ${new Date().toISOString()}

const std = @import("std");

pub const Alias = struct {
    alias: []const u8,
    domain: []const u8,
};

pub const aliases = [_]Alias{
${Q}
};

/// Resolve an alias to a domain name
pub fn resolve_alias(alias: []const u8) ?[]const u8 {
    for (aliases) |entry| {
        if (std.mem.eql(u8, entry.alias, alias)) {
            return entry.domain;
        }
    }
    return null;
}

test "Alias resolution works" {
    const resolved = resolve_alias("node");
    if (resolved) |domain| {
        try std.testing.expectEqualStrings("nodejs.org", domain);
    }
}
`;G.writeFileSync(L,W,"utf-8"),console.log(`✅ Generated Zig aliases: ${L}`),console.log(`\uD83D\uDCCA Total aliases: ${Object.keys(H).length}`)}export{S as generateZigDefinitions,P as generateZigAliases,N as compareVersions};
