"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMidUrlAppValue = exports.getMidUrlIntegrationsValue = exports.apptileContextZodSchema = exports.createDefaultLogger = exports.LogLevel = exports.Logger = exports.exec = void 0;
exports.toKebabCase = toKebabCase;
exports.logReplacer = logReplacer;
exports.createTargetName = createTargetName;
exports.toCamelCase = toCamelCase;
exports.makeHeadersWithCookie = makeHeadersWithCookie;
const node_util_1 = __importDefault(require("node:util"));
const child_process_1 = require("child_process");
const logger_1 = require("./logger");
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_1.Logger; } });
Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return logger_1.LogLevel; } });
Object.defineProperty(exports, "createDefaultLogger", { enumerable: true, get: function () { return logger_1.createDefaultLogger; } });
const zod_1 = require("zod");
exports.exec = node_util_1.default.promisify(child_process_1.exec);
function toKebabCase(anyCaseName) {
    const spacesRemoved = anyCaseName.replace(/\s/g, "-");
    const lettersOnly = spacesRemoved.replace(/[^0-9a-zA-Z\-]/g, "");
    const lowerCased = lettersOnly.toLowerCase();
    return lowerCased;
}
function logReplacer(key, value) {
    if (key === "data" && typeof value === "string" && value.length > 20) {
        return value.slice(0, 20) + "...[truncated]";
    }
    return value;
}
// If you change this, also change the type for apptile_context in baseAgent.ts
exports.apptileContextZodSchema = zod_1.z.object({
    app_id: zod_1.z.string(),
    plugin_root: zod_1.z.string().describe("Root folder of the current plugin"),
    plugins_dir: zod_1.z.string().describe("Path of the plugins directory in remoteCode"),
    supabase_creds: zod_1.z.object({
        SUPABASE_ACCESS_TOKEN: zod_1.z.string(),
        SUPABASE_REFRESH_TOKEN: zod_1.z.string(),
        ACCESS_TOKEN_EXPIRES_IN: zod_1.z.number(),
        SUPABASE_PROJECT_REF: zod_1.z.string(),
        SUPABASE_ANON_KEY: zod_1.z.string().optional(),
        APPTILE_SUPABASE_CLIENT_ID: zod_1.z.string().optional(),
        APPTILE_SUPABASE_CLIENT_SECRET: zod_1.z.string().optional()
    }).nullable()
}).nullable().describe("This should always be passed as null by the LLM. It will be set to the correct path by the tool.");
function createTargetName(anyCaseName, appId) {
    const kebab = toKebabCase(anyCaseName);
    const targetName = `${appId}_${kebab}`;
    return targetName;
}
function toCamelCase(kebabCaseName) {
    const words = kebabCaseName.split("-");
    for (let i = 1; i < words.length; ++i) {
        words[i] = words[i][0].toUpperCase() + words[i].slice(1);
    }
    return words.join("");
}
exports.default = {
    comment: "This is only exported beacuse otherwise node does not recognize this file as a module",
};
function makeHeadersWithCookie(cookie, extraHeaders) {
    //Check if this is working in production environment
    const header = Object.assign({ Accept: "application/json, text/plain, */*", "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8", Connection: "keep-alive", "If-None-Match": 'W/"5b1-kyqqkRyAnA0NO0WgKmBhdQQi7Qo"', Origin: "https://app.apptile.io", Referer: "https://app.apptile.io", "Sec-Fetch-Dest": "empty", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "cross-site", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", "sec-ch-ua": '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"', "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": '"macOS"' }, extraHeaders);
    if (process.env.NODE_ENV === "production") {
        console.log("This is a production environment");
        return {
            headers: header,
        };
    }
    else {
        console.log("This is a development environment");
        if (!cookie) {
            throw new Error("Cookie not provided");
        }
        return {
            headers: Object.assign(Object.assign({}, header), { Cookie: cookie }),
        };
    }
}
const getMidUrlIntegrationsValue = () => {
    return process.env.NODE_ENV === "production" ? "admin/api/apps" : "api/apps";
};
exports.getMidUrlIntegrationsValue = getMidUrlIntegrationsValue;
const getMidUrlAppValue = () => {
    return process.env.NODE_ENV === 'production' ? 'admin/api/v2/apps' : 'api/v2/app';
};
exports.getMidUrlAppValue = getMidUrlAppValue;
