import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
const compileCache = {};
const useCompile = function() {
    let { noCache } = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {
        noCache: false
    };
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.SchemaOptionsContext);
    const scope = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(__WEBPACK_EXTERNAL_MODULE__tachybase_schema__.SchemaExpressionScopeContext);
    return (source, ext)=>{
        let shouldCompile = false;
        let cacheKey;
        if ('string' == typeof source && source.startsWith('{{')) {
            shouldCompile = true;
            cacheKey = source;
        }
        if (source && 'object' == typeof source && !(0, __WEBPACK_EXTERNAL_MODULE_react__.isValidElement)(source)) {
            shouldCompile = true;
            cacheKey = JSON.stringify(source);
        }
        if (Array.isArray(source)) shouldCompile = true;
        if (shouldCompile) {
            const mergedScope = {
                ...options.scope,
                ...scope,
                ...ext
            };
            if (!cacheKey) return __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.compile(source, mergedScope);
            try {
                if (noCache) return __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.compile(source, mergedScope);
                compileCache[cacheKey] = compileCache[cacheKey] || __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.compile(source, mergedScope);
                return compileCache[cacheKey];
            } catch (e) {
                return __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.Schema.compile(source, mergedScope);
            }
        }
        return source;
    };
};
export { useCompile };
