import { logOptions } from "../../../otel/src/log-options.js";
import { cdcSchema, cvrSchema, getShardID } from "../types/shards.js";
import { appOptions, shardOptions, zeroOptions } from "../config/zero-config.js";
import { pgClient } from "../types/pg.js";
import { id } from "../types/sql.js";
import { decommissionShard } from "../services/change-source/pg/decommission.js";
//#region ../zero-cache/src/scripts/decommission.ts
var decommissionOptions = {
	app: { id: appOptions.id },
	shard: { num: shardOptions.num },
	upstream: {
		db: zeroOptions.upstream.db,
		type: zeroOptions.upstream.type
	},
	cvr: { db: zeroOptions.cvr.db },
	change: { db: zeroOptions.change.db },
	log: {
		level: logOptions.level,
		format: logOptions.format
	}
};
async function decommissionZero(lc, cfg) {
	const { app, shard } = cfg;
	const shardID = getShardID(cfg);
	lc.info?.(`Decommissioning app "${app.id}"`);
	if (cfg.upstream.type === "pg") {
		const upstream = pgClient(lc, cfg.upstream.db);
		await decommissionShard(lc, upstream, app.id, shard.num);
		lc.debug?.(`Cleaning up upstream metadata from ${hostPort(upstream)}`);
		await upstream.unsafe(`DROP SCHEMA IF EXISTS ${id(app.id)} CASCADE`);
		await upstream.end();
	}
	const cvr = pgClient(lc, cfg.cvr.db ?? cfg.upstream.db);
	lc.debug?.(`Cleaning up cvc data from ${hostPort(cvr)}`);
	await cvr.unsafe(`DROP SCHEMA IF EXISTS ${id(cvrSchema(shardID))} CASCADE`);
	await cvr.end();
	const cdc = pgClient(lc, cfg.change.db ?? cfg.upstream.db);
	lc.debug?.(`Cleaning up cdc data from ${hostPort(cdc)}`);
	await cdc.unsafe(`DROP SCHEMA IF EXISTS ${id(cdcSchema(shardID))} CASCADE`);
	await cdc.end();
	lc.info?.(`App "${app.id}" decommissioned`);
}
function hostPort(db) {
	const { host, port } = db.options;
	return `${host.join(",")}:${port?.at(0) ?? 5432}`;
}
//#endregion
export { decommissionOptions, decommissionZero };

//# sourceMappingURL=decommission.js.map