import { WEB_VALIDATION_RULES } from "./validator-utils/web.js";
import { MOBILE_RULES } from "./validator-utils/native_mobile.js";
import { createErrorResponse, createSuccessResponse, getRuleDescription } from "./validator-utils/common/utils.js";
import { PLATFORM } from "./constants.js";
const RULES_BY_PLATFORM = {
    [PLATFORM.MOBILE]: MOBILE_RULES,
    [PLATFORM.WEB]: WEB_VALIDATION_RULES,
};
/**
 * Validates a value against the rule set for the given platform and subtype.
 */
function validateValue(value, subtype, platform) {
    const rules = RULES_BY_PLATFORM[platform];
    const rule = rules?.[subtype];
    if (!rule?.validate)
        return createSuccessResponse();
    const result = rule.validate(value, rule);
    if (!result.valid) {
        return createErrorResponse(subtype, value, getRuleDescription(rule), platform);
    }
    return createSuccessResponse();
}
/**
 * Validates a token value against platform and subtype rules.
 *
 * @param {string|number} value   - The raw token value.
 * @param {string}        subtype - e.g. "font-size", "color", "space".
 * @param {string}        platform - "native_mobile" | "web"
 * @returns {{ valid: boolean, errorObject?: object }}
 */
export default function validateTokenValue(value, subtype, platform) {
    //Remove this for web
    if (platform !== PLATFORM.MOBILE)
        return { valid: true };
    // TODO Handling empty values
    if (value === undefined || value === null || value === "")
        return { valid: true };
    if (isTokenReference(value))
        return { valid: true };
    const normalised = typeof value === "string" ? value.trim() : String(value);
    return validateValue(normalised, subtype, platform);
}
export const isTokenReference = (value) => {
    //TODO add given token exists from existing implementation
    if (typeof value !== "string")
        return false;
    const referencePlaceholders = value.match(/{[^}]+}/g);
    if (!referencePlaceholders)
        return false;
    return referencePlaceholders.every((referenceExpression) => {
        const rawReferencePath = referenceExpression.replace(/[{}]/g, "")?.trim();
        if (!rawReferencePath)
            return false;
        return rawReferencePath.endsWith(".value");
    });
};
//# sourceMappingURL=validate-value.js.map