import { existsSync, readFileSync } from "fs";
import grabDirNames from "./grab-dir-names";

export default function grabCookieNames() {
    const { TURBOCI_DEPLOYMENT_ID_FILE } = grabDirNames();

    if (!existsSync(TURBOCI_DEPLOYMENT_ID_FILE)) {
        throw new Error(`\`${TURBOCI_DEPLOYMENT_ID_FILE}\` does not exist.`);
    }

    const deployment_id = readFileSync(TURBOCI_DEPLOYMENT_ID_FILE, "utf-8");

    const deplyment_uid_short = deployment_id.split("-").shift();

    if (!deplyment_uid_short) {
        throw new Error(`Invalid deployment_id`);
    }

    const auth_key_cookie_name = `turboci-admin-${deplyment_uid_short}-auth-key`;
    const csrf_cookie_name = `turboci-admin-${deplyment_uid_short}-csrf`;

    return { auth_key_cookie_name, csrf_cookie_name };
}
