import { Property } from "@superblocksteam/library-shared";
import { sbEntitySymbol, SbEntityType, StateVarPersistence, } from "@superblocksteam/library-shared/types";
import { isFunctionNode } from "../jsx-utils.js";
import { parseNodeProperties } from "../properties.js";
import { getIdentiferName, getObjectPropertyByKey, nodeToValue, } from "../util.js";
const parseStateVarProperties = (argsPath) => {
    const properties = {};
    for (const property of argsPath.get("properties")) {
        if (property.isObjectProperty()) {
            const name = getIdentiferName(property.get("key").node);
            const value = parseNodeProperties(property.get("value"), {
                bindings: [],
                jsxVariables: [],
                libraryComponents: [],
            });
            // This one is a special case
            if (name === "persistence" && value) {
                properties.persistence = Property.Expression(value.value);
            }
            else if (name && value) {
                properties[name] = value;
            }
        }
    }
    return properties;
};
const parseTimerProperties = (argsPath) => {
    if (!argsPath.isObjectExpression()) {
        return {};
    }
    const intervalMs = getObjectPropertyByKey(argsPath, "intervalMs");
    const startOnPageLoad = getObjectPropertyByKey(argsPath, "startOnPageLoad");
    const onFire = getObjectPropertyByKey(argsPath, "onFire");
    const id = getObjectPropertyByKey(argsPath, "id");
    return {
        intervalMs: Property.Static(intervalMs
            ? nodeToValue(intervalMs.get("value").node)
            : 300000),
        startOnPageLoad: Property.Static(startOnPageLoad
            ? nodeToValue(startOnPageLoad.get("value").node)
            : false),
        onFire: Property.Function(onFire &&
            (isFunctionNode(onFire.get("value")) ||
                onFire.get("value").isIdentifier())
            ? onFire.get("value").toString()
            : "() => {}"),
        id: id ? nodeToValue(id.get("value").node) : undefined,
    };
};
const parseSbApiProperties = (argsPath) => {
    if (!argsPath.isObjectExpression()) {
        return {};
    }
    const onSuccess = getObjectPropertyByKey(argsPath, "onSuccess");
    const onError = getObjectPropertyByKey(argsPath, "onError");
    const id = getObjectPropertyByKey(argsPath, "id");
    const idProperty = id
        ? nodeToValue(id.get("value").node)
        : undefined;
    return {
        id: idProperty,
        onSuccess: Property.Function(onSuccess &&
            (isFunctionNode(onSuccess.get("value")) ||
                onSuccess.get("value").isIdentifier())
            ? onSuccess.get("value").toString()
            : "() => {}"),
        onError: Property.Function(onError &&
            (isFunctionNode(onError.get("value")) ||
                onError.get("value").isIdentifier())
            ? onError.get("value").toString()
            : "() => {}"),
    };
};
export function toValueEntity(nodePath, { key, id }) {
    if (!id) {
        console.error("No ID supplied for entity", key);
        return null;
    }
    if (!nodePath.isCallExpression() || !nodePath.get("callee").isIdentifier()) {
        return null;
    }
    const entityType = nodePath.get("callee").node.name;
    const argsPath = nodePath.get("arguments")[0];
    if (!argsPath.isObjectExpression()) {
        return null;
    }
    switch (entityType) {
        case "StateVar": {
            const args = parseStateVarProperties(argsPath);
            return {
                [sbEntitySymbol]: true,
                type: SbEntityType.VARIABLE,
                name: key,
                defaultValue: args.defaultValue ?? Property.Static(""),
                persistence: args.persistence ??
                    Property.Expression(StateVarPersistence.TEMPORARY),
                value: args.defaultValue ?? Property.Static(""),
                id,
            };
        }
        case "Timer": {
            const args = parseTimerProperties(argsPath);
            return {
                [sbEntitySymbol]: true,
                type: SbEntityType.TIMER,
                name: key,
                intervalMs: args?.intervalMs ?? Property.Static(300000),
                startOnPageLoad: args?.startOnPageLoad ?? Property.Static(false),
                onFire: args?.onFire ?? Property.Function("() => {}"),
                id: args.id ?? id,
            };
        }
        case "SbApi": {
            const args = parseSbApiProperties(argsPath);
            return {
                [sbEntitySymbol]: true,
                type: SbEntityType.API,
                id,
                name: key,
                response: Property.Static(""),
                error: Property.Static(""),
                onSuccess: args.onSuccess ?? Property.Function("() => {}"),
                onError: args.onError ?? Property.Function("() => {}"),
            };
        }
    }
    return null;
}
//# sourceMappingURL=to-value-entity.js.map