"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useDiscoveryModels: ()=>useDiscoveryModels
});
const external_react_namespaceObject = require("react");
function useDiscoveryModels(ctx) {
    const [models, setModels] = (0, external_react_namespaceObject.useState)([]);
    const [loading, setLoading] = (0, external_react_namespaceObject.useState)(false);
    const [error, setError] = (0, external_react_namespaceObject.useState)(null);
    const abortRef = (0, external_react_namespaceObject.useRef)(null);
    const fetchModels = (0, external_react_namespaceObject.useCallback)(async ()=>{
        if (!ctx) return;
        abortRef.current?.abort();
        const ctrl = new AbortController();
        abortRef.current = ctrl;
        setLoading(true);
        setError(null);
        const base = (ctx.baseUrl ?? '').replace(/\/$/, '');
        const params = new URLSearchParams();
        if (false === ctx.onlyAvailableModels) params.set('onlyAvailableModels', 'false');
        const qs = params.toString();
        const url = `${base}/api/discovery${qs ? `?${qs}` : ''}`;
        const headers = {
            Authorization: `Bearer ${ctx.token}`,
            Accept: 'application/json',
            'X-UiPath-Internal-AccountId': ctx.accountId,
            'X-UiPath-Internal-TenantId': ctx.tenantId,
            'X-UiPath-LlmGateway-RequestingProduct': ctx.requestingProduct,
            'X-UiPath-LlmGateway-RequestingFeature': ctx.requestingFeature
        };
        if (ctx.operationCode) headers['X-UiPath-LlmGateway-OperationCode'] = ctx.operationCode;
        if (ctx.folderKey) headers['X-UiPath-FolderKey'] = ctx.folderKey;
        if (ctx.fromDedicatedCloud) headers['X-UiPath-LlmGateway-FromDedicatedCloud'] = 'true';
        try {
            const res = await fetch(url, {
                headers,
                signal: ctrl.signal
            });
            if (!res.ok) throw new Error(`Discovery API ${res.status} ${res.statusText}`);
            const data = await res.json();
            const list = Array.isArray(data) ? data : data.models ?? [];
            if (!ctrl.signal.aborted) setModels(normalizeKeys(list));
        } catch (err) {
            if (err?.name === 'AbortError') return;
            if (!ctrl.signal.aborted) setError(err instanceof Error ? err : new Error(String(err)));
        } finally{
            if (!ctrl.signal.aborted) setLoading(false);
        }
    }, [
        ctx
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (!ctx) {
            abortRef.current?.abort();
            setModels([]);
            setLoading(false);
            setError(null);
            return;
        }
        fetchModels();
        return ()=>abortRef.current?.abort();
    }, [
        ctx,
        fetchModels
    ]);
    return {
        models,
        loading,
        error,
        refetch: fetchModels
    };
}
function camelizeDeep(value) {
    if (Array.isArray(value)) return value.map(camelizeDeep);
    if (!value || 'object' != typeof value) return value;
    const camel = Object.create(null);
    for (const [k, v] of Object.entries(value))camel[k.charAt(0).toLowerCase() + k.slice(1)] = camelizeDeep(v);
    return camel;
}
function normalizeKeys(list) {
    return list.map((raw)=>camelizeDeep(raw));
}
exports.useDiscoveryModels = __webpack_exports__.useDiscoveryModels;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useDiscoveryModels"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
