/**
 * Shared types for `@agentuity/storage`.
 *
 * Both the Bun-backed and Node-backed implementations conform to the
 * `S3ClientLike` interface. Callers should program against this surface
 * rather than backend-specific shapes, so that swapping the backend (or
 * letting the package conditionally pick one) is transparent.
 */
/**
 * Resolve the canonical virtual-hosted endpoint URL from a
 * `BucketConfig`, applying the `endpoint` vs `host`+`bucket` rules.
 *
 * Always returns a fully-qualified URL with a scheme (defaults to
 * `https://` when missing). Throws on invalid configurations rather
 * than silently picking a default — misconfigured storage usually
 * means the user forgot to provision/inject env vars.
 */
export function resolveEndpoint(bucket) {
    if (bucket.endpoint && (bucket.host || bucket.bucket)) {
        throw new Error('BucketConfig accepts either `endpoint` or `host`+`bucket`, not both.');
    }
    if (bucket.endpoint) {
        return bucket.endpoint.startsWith('http') ? bucket.endpoint : `https://${bucket.endpoint}`;
    }
    if (!bucket.host || !bucket.bucket) {
        throw new Error('BucketConfig requires either `endpoint` or both `host` and `bucket`.');
    }
    // Strip scheme + trailing slashes from `host` so callers can pass
    // either `t3.storage.dev` or `https://t3.storage.dev/`.
    const hostOnly = bucket.host.replace(/^https?:\/\//, '').replace(/\/+$/, '');
    return `https://${bucket.bucket}.${hostOnly}`;
}
/**
 * Build a `BucketConfig` from environment variables.
 *
 * Uses the standard AWS naming scheme — same one the AWS SDKs and CLI
 * read — so no key translation is needed when targeting AWS or any
 * S3-compatible service. This matches what the Agentuity platform
 * injects when a bucket is provisioned:
 *
 *   `AWS_ENDPOINT` (shared host) + `AWS_BUCKET` (bucket name) +
 *   `AWS_ACCESS_KEY_ID` + `AWS_SECRET_ACCESS_KEY`,
 *   optional `AWS_REGION`.
 *
 * Throws a descriptive error when the required vars are missing so
 * callers can surface a useful message at startup or first request.
 *
 * Pass an explicit `env` (defaults to `process.env`) to make this
 * unit-testable without mutating global state.
 */
export function bucketConfigFromEnv(env = process.env) {
    const rawEndpoint = env.AWS_ENDPOINT;
    const bucket = env.AWS_BUCKET;
    const access = env.AWS_ACCESS_KEY_ID;
    const secret = env.AWS_SECRET_ACCESS_KEY;
    if (rawEndpoint && bucket && access && secret) {
        return {
            host: normalizeHost(rawEndpoint, bucket),
            bucket,
            access_key: access,
            secret_key: secret,
            region: env.AWS_REGION,
        };
    }
    throw new Error('Storage env vars are not set. Expected AWS_ENDPOINT + AWS_BUCKET + ' +
        'AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY. Provision an Agentuity ' +
        'bucket or set them manually.');
}
function normalizeHost(endpoint, bucket) {
    const host = endpoint.replace(/^https?:\/\//, '').replace(/\/+$/, '');
    const bucketPrefix = `${bucket}.`;
    return host.startsWith(bucketPrefix) ? host.slice(bucketPrefix.length) : host;
}
//# sourceMappingURL=types.js.map