import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__async_data_provider_index_mjs__ from "../../../../async-data-provider/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__ from "../../../../record-provider/index.mjs";
const SQLRequestProvider = (props)=>{
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const form = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.useForm)();
    const record = (0, __WEBPACK_EXTERNAL_MODULE__record_provider_index_mjs__.useRecord)();
    let { manual } = props;
    manual = void 0 === manual || manual;
    const result = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)((sql)=>api.resource('sqlCollection').execute({
            values: {
                sql
            }
        }).then((res)=>{
            var _res_data;
            return (null == res ? void 0 : null === (_res_data = res.data) || void 0 === _res_data ? void 0 : _res_data.data) || {
                data: [],
                fields: [],
                sources: []
            };
        }), {
        manual: true,
        onSuccess: (data)=>{
            const { sources } = data;
            const formSources = form.values.sources || [];
            form.setValuesIn('sources', Array.from(new Set([
                ...formSources,
                ...sources
            ])));
        }
    });
    const { run } = result;
    const sql = form.values.sql || record.sql;
    const first = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(true);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (sql && first.current) run(sql);
        first.current = false;
    }, [
        manual,
        run,
        sql
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__async_data_provider_index_mjs__.AsyncDataProvider, {
        value: result,
        children: props.children
    });
};
export { SQLRequestProvider };
