"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.maybeFilePath = maybeFilePath;
exports.normalizeInput = normalizeInput;
exports.hashPrompt = hashPrompt;
const lodash_1 = __importDefault(require("lodash"));
const createHash_1 = require("../util/createHash");
const constants_1 = require("./constants");
/**
 * Determines if a string is a valid file path.
 * @param str - The string to check.
 * @returns True if the string is a valid file path, false otherwise.
 */
function maybeFilePath(str) {
    if (typeof str !== 'string') {
        throw new Error(`Invalid input: ${JSON.stringify(str)}`);
    }
    const forbiddenSubstrings = ['\n', 'portkey://', 'langfuse://', 'helicone://'];
    if (forbiddenSubstrings.some((substring) => str.includes(substring))) {
        return false;
    }
    return (str.startsWith('file://') ||
        constants_1.VALID_FILE_EXTENSIONS.some((ext) => {
            const tokens = str.split(':'); // str may be file.js:functionName
            // Checks if the second to last token or the last token ends with the extension
            return tokens.pop()?.endsWith(ext) || tokens.pop()?.endsWith(ext);
        }) ||
        str.charAt(str.length - 3) === '.' ||
        str.charAt(str.length - 4) === '.' ||
        str.includes('*') ||
        str.includes('/') ||
        str.includes('\\'));
}
/**
 * Normalizes the input prompt to an array of prompts, rejecting invalid and empty inputs.
 * @param promptPathOrGlobs - The input prompt.
 * @returns The normalized prompts.
 * @throws If the input is invalid or empty.
 */
function normalizeInput(promptPathOrGlobs) {
    if (!promptPathOrGlobs ||
        ((typeof promptPathOrGlobs === 'string' || Array.isArray(promptPathOrGlobs)) &&
            promptPathOrGlobs.length === 0)) {
        throw new Error(`Invalid input prompt: ${JSON.stringify(promptPathOrGlobs)}`);
    }
    if (typeof promptPathOrGlobs === 'string') {
        return [
            {
                raw: promptPathOrGlobs,
            },
        ];
    }
    if (Array.isArray(promptPathOrGlobs)) {
        return promptPathOrGlobs.map((promptPathOrGlob, index) => {
            if (typeof promptPathOrGlob === 'string') {
                return {
                    raw: promptPathOrGlob,
                };
            }
            return {
                raw: promptPathOrGlob.raw || promptPathOrGlob.id,
                ...promptPathOrGlob,
            };
        });
    }
    if (typeof promptPathOrGlobs === 'object' && Object.keys(promptPathOrGlobs).length) {
        /* NOTE: This format is considered legacy and has been deprecated. Example:
          {
            'prompts.txt': 'foo1',
            'prompts.py': 'foo2',
          }
          */
        return Object.entries(promptPathOrGlobs).map(([raw, key]) => ({
            label: key,
            raw,
        }));
    }
    // numbers, booleans, etc
    throw new Error(`Invalid input prompt: ${JSON.stringify(promptPathOrGlobs)}`);
}
function sha256ArrayOrObject(value) {
    if (typeof value === 'string' && value.length === 0) {
        return null;
    }
    if (Array.isArray(value) || (typeof value === 'object' && value !== null && value !== undefined)) {
        return (0, createHash_1.sha256)(JSON.stringify(value));
    }
    return (0, createHash_1.sha256)(value);
}
function hashPrompt(prompt) {
    return lodash_1.default.isEmpty(prompt?.id) === false || lodash_1.default.isEmpty(prompt?.label) === false
        ? sha256ArrayOrObject(prompt?.label ?? '')
        : sha256ArrayOrObject(prompt?.raw ?? '');
}
//# sourceMappingURL=utils.js.map