import { nanoid } from "nanoid";
import { parse } from "../../../../shared/src/valita.js";
import { object, string } from "@badrap/valita";
const containerMetadataSchema = object({ ["TaskARN"]: string() });
async function getTaskID(lc) {
  const containerURI = process.env["ECS_CONTAINER_METADATA_URI_V4"];
  if (containerURI) {
    try {
      const resp = await fetch(`${containerURI}`);
      const metadata = await resp.json();
      lc.info?.(`Container metadata`, { metadata });
    } catch (e) {
      lc.warn?.("unable to lookup container metadata", e);
    }
    try {
      const resp = await fetch(`${containerURI}/task`);
      const metadata = parse(
        await resp.json(),
        containerMetadataSchema,
        "passthrough"
      );
      lc.info?.(`Task metadata`, { metadata });
      const { TaskARN: taskID } = metadata;
      const lastSlash = taskID.lastIndexOf("/");
      return taskID.substring(lastSlash + 1);
    } catch (e) {
      lc.warn?.("unable to determine task ID. falling back to random ID", e);
    }
  }
  return nanoid();
}
export {
  getTaskID
};
//# sourceMappingURL=runtime.js.map
