import { readFile } from "node:fs/promises";
import { EnvironmentType, } from "./interfaces.portal.js";
function npmNameToUrlSafe(name) {
    // Scoped packages: @scope/pkg -> scope-pkg
    if (name.startsWith("@")) {
        return name.slice(1).replace("/", "-");
    }
    // Other names: just URI encode to be safe
    return encodeURIComponent(name.toLowerCase());
}
export async function getLocalEditorConfig({ deploymentUrl, sourceDirectory, logger, }) {
    let pkgJson;
    try {
        pkgJson = (await readFile(`${sourceDirectory}/package.json`, "utf-8").then(JSON.parse));
    }
    catch (err) {
        logger.warn({ err }, "Failed to load package.json");
    }
    const projectName = npmNameToUrlSafe(pkgJson?.name ?? "project");
    const account = {
        id: "dev",
        name: "dev",
        label: "Local Development",
        createdOn: new Date().toISOString(),
        isSharingEnabled: false,
        isMfaEnforced: false,
        isSsoAutoJoinEnabled: false,
        requireSsoAuth: false,
        plan: "FREE_V1",
        supportPin: undefined,
        logoUrl: null,
        entitlements: {
            customDomains: 0,
            aliasDomains: false,
            devPortalZuploBranding: true,
            egressGbPerMonth: 100,
            kvStoreEnabled: true,
            numberOfApiKeys: 100,
            numberOfProjects: 10,
            numberOfUsers: 5,
            requestsPerMonth: 1000000,
            advancedAnalyticsEnabled: false,
            analyticsHistoryDays: 1,
            premiumSupport: false,
            emergencyPhoneSupport: false,
            rbacEnabled: false,
            mfaEnforcement: false,
            enterpriseSingleSignOn: false,
            auditLogsEnabled: false,
        },
        customDomainsInUse: 0,
        tenantId: null,
    };
    const project = {
        sourceControlProvider: "GITHUB",
        accountName: account.name,
        name: projectName,
        productType: "standard",
        createdOn: new Date().toISOString(),
        sourceRepoUrl: null,
        defaultBranch: null,
        hasWorkingCopy: true,
        hasCurrentUserWorkingCopy: true,
        deployedEnvironmentCount: 0,
        isProjectMember: true,
        id: "local-project-id",
        type: "managed-edge",
    };
    const environmentName = `local-${projectName}`;
    const environment = {
        name: environmentName,
        label: "local",
        urlParameter: environmentName,
        createdOn: Date.now(),
        accountName: account.name,
        projectName: project.name,
        state: "STARTED",
        message: null,
        branchName: null,
        environmentType: EnvironmentType.WORKING_COPY,
        customDomain: undefined,
        updatedOn: Date.now(),
        deploymentVersion: {
            id: "local",
            createdOn: new Date().toISOString(),
            repoUrl: "",
            branch: "",
            sha: "",
            url: deploymentUrl,
            stages: [
                {
                    id: "local",
                    status: "success",
                    stage: "api",
                    url: deploymentUrl,
                },
            ],
        },
    };
    const apeContext = {
        account,
        project,
        environment,
    };
    // Build the default path using the same names as the context
    const defaultPath = `/${account.name}/${project.name}/${environment.name}`;
    const localEditorConfig = { apeContext, defaultPath };
    return localEditorConfig;
}
//# sourceMappingURL=config.js.map