"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryResolveZone = tryResolveZone;
exports.resolveZone = resolveZone;
const ampli_config_js_1 = require("./ampli-config.js");
const ampli_settings_js_1 = require("../utils/ampli-settings.js");
/**
 * Attempt to resolve the zone from explicit signals only (no fallback).
 * Returns null when no signal is available — callers that require a
 * definite regional intent (e.g. direct signup, which POSTs to
 * region-specific provisioning endpoints) should treat `null` as "user
 * must be asked" rather than silently defaulting to US.
 */
function tryResolveZone(session) {
    // Tier 1: explicit user intent for this run.
    if (session.region != null) {
        return session.region;
    }
    // Tier 2: project config (ampli.json Zone).
    const projectConfig = (0, ampli_config_js_1.readAmpliConfig)(session.installDir);
    if (projectConfig.ok && projectConfig.config.Zone != null) {
        return projectConfig.config.Zone;
    }
    // Tier 3: stored user's home zone. `getStoredUser` returns at most one
    // record (real or pending); the home-zone semantics are identical for
    // both — a pending user during SUSI has the same regional intent as a
    // real user restored from ~/.ampli.json.
    const storedUser = (0, ampli_settings_js_1.getStoredUser)();
    if (storedUser?.zone != null) {
        return storedUser.zone;
    }
    return null;
}
function resolveZone(session, fallback, options) {
    if (!options.readDisk) {
        return session.region ?? fallback;
    }
    return tryResolveZone(session) ?? fallback;
}
