import { env } from "../../../../env.js";
import { LocalBlobStorageProvider } from "./local-provider.js";
import { S3BlobStorageProvider } from "./s3-provider.js";
import { VercelBlobStorageProvider } from "./vercel-blob-provider.js";

//#region src/domains/run/services/blob-storage/index.ts
let instance = null;
/**
* Returns the configured blob storage provider with precedence:
* 1) S3 when BLOB_STORAGE_S3_BUCKET is set
* 2) Vercel when BLOB_READ_WRITE_TOKEN is set
* 3) Local fallback otherwise
*
* Stored keys do not encode backend; changing provider strategy after uploads
* can cause existing media URLs to 404.
*/
function getBlobStorageProvider() {
	if (!instance) if (env.BLOB_STORAGE_S3_BUCKET?.trim()) instance = new S3BlobStorageProvider();
	else if (env.BLOB_READ_WRITE_TOKEN?.trim()) instance = new VercelBlobStorageProvider();
	else instance = new LocalBlobStorageProvider();
	return instance;
}
const BLOB_URI_PREFIX = "blob://";
function isBlobUri(uri) {
	return uri.startsWith(BLOB_URI_PREFIX);
}
function toBlobUri(key) {
	return `${BLOB_URI_PREFIX}${key}`;
}
function fromBlobUri(uri) {
	if (!isBlobUri(uri)) throw new Error(`Not a blob URI: ${uri}`);
	return uri.slice(7);
}

//#endregion
export { BLOB_URI_PREFIX, fromBlobUri, getBlobStorageProvider, isBlobUri, toBlobUri };