import { createPhysicalName } from "alchemy/PhysicalName";
import * as Effect from "effect/Effect";
import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
export const MAX_ID_LENGTH = 63;
export const MAX_DISPLAY_NAME_LENGTH = 128;
export const MAX_VIEW_DISPLAY_NAME_LENGTH = 64;
export const MAX_LABEL_LENGTH = 256;
export const lastSegment = (value: string) => {
const trimmed = value.replace(/\/+$/, "");
const parts = trimmed.split("/");
return parts[parts.length - 1] || trimmed;
};
export const locationOf = (name: string, fallback = "") => {
const parts = name.split("/");
const index = parts.indexOf("locations");
return index >= 0 ? (parts[index + 1] ?? fallback) : fallback;
};
export const parentOf = (name: string) => {
const parts = name.split("/").filter((part) => part.length > 0);
return parts.slice(0, -2).join("/");
};
export const locationParent = (project: string, location: string) =>
`projects/${project}/locations/${location}`;
export const normalizeLocation = (location: string) => lastSegment(location).toLowerCase();
export const sameText = (left: string | undefined, right: string | undefined) =>
(left ?? "") === (right ?? "");
export const jsonEqual = (left: unknown, right: unknown) =>
JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
export const updateMaskOf = (...fields: Array<string | undefined>) =>
fields.filter((field): field is string => field !== undefined).join(",");
export const toResourceId = (
id: string,
requested: string | undefined,
existing: string | undefined,
maxLength = MAX_ID_LENGTH,
) =>
Effect.gen(function* () {
if (requested !== undefined) return requested;
if (existing !== undefined) return existing;
const generated = yield* createPhysicalName({
id,
maxLength,
lowercase: true,
});
const next = /^[a-z]/.test(generated) ? generated : `c${generated}`.slice(0, maxLength);
return next.length >= 4 ? next : `${next}xxxx`.slice(0, maxLength);
});
const markerOf = (labels: Record<string, string>, stack: string, stage: string, id: string) =>
`[alchemy ${alchemyLabelKeys.stack}=${stack} ${alchemyLabelKeys.stage}=${stage} ${alchemyLabelKeys.id}=${id}]`;
const fitMarker = (labels: Record<string, string>, maxLength: number) => {
let stack = labels[alchemyLabelKeys.stack] ?? "x";
let stage = labels[alchemyLabelKeys.stage] ?? "x";
let id = labels[alchemyLabelKeys.id] ?? "x";
let marker = markerOf(labels, stack, stage, id);
while (marker.length > maxLength && (stack.length > 1 || stage.length > 1 || id.length > 1)) {
if (stack.length >= stage.length && stack.length >= id.length) {
stack = stack.slice(0, -1);
} else if (stage.length >= id.length) {
stage = stage.slice(0, -1);
} else {
id = id.slice(0, -1);
}
marker = markerOf(labels, stack, stage, id);
}
return marker.slice(0, maxLength);
};
export const encodeOwnership = (
labels: Record<string, string>,
text: string | undefined,
): string => {
const marker = fitMarker(labels, 8000);
const trimmed = text?.trim();
return trimmed && trimmed.length > 0 ? `${marker}\n${trimmed}` : marker;
};
export const encodeOwnershipLine = (
labels: Record<string, string>,
text: string | undefined,
maxLength = MAX_DISPLAY_NAME_LENGTH,
): string => {
const trimmed = text?.replace(/[\r\n]+/g, " ").trim();
if (!trimmed) return fitMarker(labels, maxLength);
// The shortest parseable marker (one-character stack, stage, and id);
// anything shorter loses its closing bracket.
const minMarker = markerOf(labels, "x", "x", "x").length;
const reserved = Math.min(trimmed.length + 1, Math.max(0, maxLength - minMarker));
const marker = fitMarker(labels, maxLength - reserved);
return `${marker} ${trimmed}`.slice(0, maxLength);
};
export const parseOwnership = (
text: string | undefined,
): {
labels: Record<string, string>;
text: string | undefined;
} => {
if (!text?.startsWith("[alchemy ")) {
return { labels: {}, text };
}
const end = text.indexOf("]");
if (end < 0) return { labels: {}, text };
const labels: Record<string, string> = {};
for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
const eq = part.indexOf("=");
if (eq > 0) {
labels[part.slice(0, eq)] = part.slice(eq + 1);
}
}
const rest = text.slice(end + 1).replace(/^[\s\n]+/, "");
return { labels, text: rest.length > 0 ? rest : undefined };
};
export const hasOwnershipMarker = (text: string | undefined) =>
Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-"));
const prefixMatch = (expected: string, observed: string) =>
expected === observed || expected.startsWith(observed) || observed.startsWith(expected);
export const ownedByAlchemy = (id: string, text: string | undefined) =>
Effect.gen(function* () {
const expected = yield* createInternalLabels(id);
const { labels } = parseOwnership(text);
if (!hasOwnershipMarker(text)) return false;
const exact = yield* hasAlchemyLabels(id, labels);
if (exact) return true;
return (
prefixMatch(expected[alchemyLabelKeys.stack] ?? "", labels[alchemyLabelKeys.stack] ?? "") &&
prefixMatch(expected[alchemyLabelKeys.stage] ?? "", labels[alchemyLabelKeys.stage] ?? "") &&
prefixMatch(expected[alchemyLabelKeys.id] ?? "", labels[alchemyLabelKeys.id] ?? "")
);
});
export const internalLabels = (id: string) => createInternalLabels(id);
export const sameJson = jsonEqual;