import { useCallback } from 'react';
import { apiMutate } from '../utilities/useAxiosMutate';
import { ConfigService } from '../configService';
/**
 * Pure data-fetching helper extracted from useCrudActions so ActionWrapper and
 * other entry-chunk consumers can use it without statically pulling in the
 * heavy form-rendering JSX (DateTimeField → @date-io/date-fns → date-fns,
 * FluentColorPicker → react-color, etc.) that the rest of useCrudActions
 * needs.
 */
export function useCrudForeignKeyData() {
    const getFKData = useCallback(async (fkMethods, setForeignKeyData, enqueueSnackbar, displayToasts) => {
        const fkData = [];
        const promises = [];
        const promiseDetails = [];
        fkMethods.forEach((method) => {
            var _a, _b, _c, _d, _e;
            if (method && ((_a = method === null || method === void 0 ? void 0 : method.entity) === null || _a === void 0 ? void 0 : _a.unityBaseGetAllMethod)) {
                promiseDetails.push({
                    url: ConfigService.integrationV1ApiUrl +
                        `/apimethod/executeQueryWithParameters/${(_c = (_b = method === null || method === void 0 ? void 0 : method.entity) === null || _b === void 0 ? void 0 : _b.unityBaseGetAllMethod) === null || _c === void 0 ? void 0 : _c.apiMethodId}`,
                    fieldName: method.fkName,
                    name: method.entity.entityName,
                });
                promises.push(apiMutate(ConfigService.integrationV1ApiUrl, `apimethod/executeQueryWithParameters/${(_e = (_d = method === null || method === void 0 ? void 0 : method.entity) === null || _d === void 0 ? void 0 : _d.unityBaseGetAllMethod) === null || _e === void 0 ? void 0 : _e.apiMethodId}`, { method: 'post' }, { data: {} }));
            }
            else if (displayToasts) {
                enqueueSnackbar(`${method.entity.entityName}'s Get-All method needs to be promoted`, {
                    variant: 'error',
                    anchorOrigin: { vertical: 'top', horizontal: 'center' },
                });
            }
            else {
                console.warn(`${method.entity.entityName}'s Get-All method needs to be promoted`);
            }
        });
        Promise.all(promises).then((resp) => {
            resp.forEach((response) => {
                if (response.status === 200 && (response === null || response === void 0 ? void 0 : response.data)) {
                    const respData = response.data.map((d) => {
                        var _a;
                        return ({
                            id: d.id,
                            name: (_a = d.name) !== null && _a !== void 0 ? _a : d.id,
                        });
                    });
                    const promiseDetail = promiseDetails.find((p) => {
                        var _a;
                        return p.url === ((_a = response === null || response === void 0 ? void 0 : response.config) === null || _a === void 0 ? void 0 : _a.url);
                    });
                    fkData.push({
                        fieldName: promiseDetail.fieldName,
                        name: promiseDetail.fieldName,
                        data: respData,
                    });
                }
            });
            setForeignKeyData(fkData);
        });
    }, []);
    return { getFKData };
}
//# sourceMappingURL=crudForeignKeyData.js.map