import { parse } from "@babel/parser";
import { SbEntityType, StateVarPersistence, } from "@superblocksteam/library-shared/types";
import { containsBindingsAnywhere } from "@superblocksteam/shared";
const isJavaScriptCode = (value) => {
    if (containsBindingsAnywhere(value)) {
        return false;
    }
    if (value.length === 0) {
        return false;
    }
    // Check for JavaScript-specific patterns that indicate actual code, not just identifiers
    const jsPatterns = [
        /\(/,
        /\./,
        /\[/,
        /{/,
        /\b(function|const|let|var|if|else|for|while)\b/,
        /=>/,
    ];
    if (!jsPatterns.some((pattern) => pattern.test(value))) {
        return false;
    }
    try {
        parse(value, {
            sourceType: "module",
            allowReturnOutsideFunction: true,
            plugins: ["typescript", "jsx"],
        });
        return true;
    }
    catch {
        return false;
    }
};
const toCodeProperty = (value) => {
    if (typeof value === "string") {
        if (isJavaScriptCode(value)) {
            return `computed(() => {
        ${value}
      })`;
        }
        if (containsBindingsAnywhere(value)) {
            return `SB\`${value}\``;
        }
        return `"${value}"`;
    }
    else {
        return JSON.stringify(value);
    }
};
export function toCodeEntity(entity) {
    switch (entity.type) {
        case SbEntityType.TIMER:
            return `Timer({
        intervalMs: ${entity.intervalMs ?? 300000},
        startOnPageLoad: ${entity.startOnPageLoad},
        onFire: ${entity.onFire ?? "() => {}"},
      })`;
        case SbEntityType.VARIABLE:
            return `StateVar({
        defaultValue: ${toCodeProperty(entity.defaultValue)},
        persistence: ${entity.persistence === StateVarPersistence.LOCAL_STORAGE
                ? "StateVarPersistence.LOCAL_STORAGE"
                : "StateVarPersistence.TEMPORARY"}
      })`;
        case SbEntityType.API:
            return `SbApi({
        onError: ${entity.onError ?? "() => {}"}, 
        onSuccess: ${entity.onSuccess ?? "() => {}"},
      })`;
        case SbEntityType.EVENT:
            return `SbEvent({})`;
        default:
            throw new Error(`Unknown entity type: ${entity.type}`);
    }
}
//# sourceMappingURL=to-code-entity.js.map