var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { useCallback, useEffect, useMemo, useState } from 'react';
import { ActivityFilter, collectAllTransitiveEntitiesUris, getEntitiesMapForTransitiveMatches, getMatchesForDataTenantEntity, getMatchesFromDataTenants, getTransitiveMatches, isDataTenantEntity, isTempUri, withDtssPotentialItems, addGlobalFilterToQuery, addGetTransitiveMatchesMaskingOptions } from '@reltio/mdm-sdk';
import { usePagingSimulator } from '../usePagingSimulator';
import { useSafePromise } from '../useSafePromise';
import { useMdmDataTenants, useMdmDtssPath, useMdmGlobalSearchRequestOptions, useMdmMetadata, useMdmTenant, useMdmShowMasking } from '../../contexts/MdmModuleContext';
export var useMatchesLoader = function (_a) {
    var enabled = _a.enabled, entity = _a.entity, filter = _a.filter, rules = _a.rules, onFinishLoading = _a.onFinishLoading, onStartLoading = _a.onStartLoading, options = _a.options, page = _a.page, rowsPerPage = _a.rowsPerPage, sorting = _a.sorting, markMatchedValues = _a.markMatchedValues;
    var _b = sorting || {}, order = _b.order, field = _b.field;
    var _c = useState(0), total = _c[0], setTotal = _c[1];
    var _d = useState([]), matches = _d[0], setMatches = _d[1];
    var _e = useState({}), entitiesMap = _e[0], setEntitiesMap = _e[1];
    var metadata = useMdmMetadata();
    var tenant = useMdmTenant();
    var dtssPath = useMdmDtssPath();
    var dataTenants = useMdmDataTenants();
    var showMasking = useMdmShowMasking();
    var _f = useMdmGlobalSearchRequestOptions() || {}, activityFilter = _f.activityFilter, globalFilter = _f.globalFilter;
    var activeness = typeof options.showInactiveEntities === 'boolean'
        ? options.showInactiveEntities
            ? ActivityFilter.ALL
            : ActivityFilter.ACTIVE_ONLY
        : activityFilter;
    var entityUri = entity === null || entity === void 0 ? void 0 : entity.uri;
    var dtssParams = useMemo(function () { return ({
        dtssPath: dtssPath,
        dataTenants: dataTenants,
        entity: entity,
        customerTenant: tenant,
        dtssRequest: getMatchesFromDataTenants
    }); }, 
    //eslint-disable-next-line react-hooks/exhaustive-deps
    [dtssPath, dataTenants, entityUri, tenant]);
    //eslint-disable-next-line react-hooks/exhaustive-deps
    var getTransitiveMatchesWithDtss = useCallback(withDtssPotentialItems(dtssParams, getTransitiveMatches), [
        dtssParams
    ]);
    var getTransitiveMatchesWithPaging = usePagingSimulator(getTransitiveMatchesWithDtss);
    var getPagedMatchesForDataTenantEntity = usePagingSimulator(getMatchesForDataTenantEntity);
    var safePromise = useSafePromise();
    var getMatches = useCallback(function (force) {
        if (force === void 0) { force = false; }
        if (!enabled || isTempUri(entityUri)) {
            setTotal(0);
            setMatches([]);
            setEntitiesMap({});
            return;
        }
        onStartLoading === null || onStartLoading === void 0 ? void 0 : onStartLoading();
        safePromise(isDataTenantEntity(entity)
            ? getPagedMatchesForDataTenantEntity({
                dtssPath: dtssPath,
                entity: entity,
                customerTenant: tenant,
                offset: page * rowsPerPage,
                max: rowsPerPage,
                force: force
            })
            : getTransitiveMatchesWithPaging(__assign(__assign({}, addGetTransitiveMatchesMaskingOptions(metadata, {
                entityUri: entityUri,
                max: rowsPerPage,
                filter: addGlobalFilterToQuery(globalFilter, filter) || '',
                rules: rules,
                sort: field,
                order: order,
                markMatchedValues: markMatchedValues,
                deep: options.showTransitiveMatches ? undefined : 1,
                activeness: activeness
            }, showMasking)), { offset: page * rowsPerPage, force: force })))
            .then(function (result) {
            var response = result.response, total = result.total, originalResponse = result.originalResponse;
            setTotal(total);
            var matchUris = collectAllTransitiveEntitiesUris(originalResponse);
            if (matchUris.length > 0) {
                setEntitiesMap(getEntitiesMapForTransitiveMatches(originalResponse));
            }
            setMatches(response);
        })
            .catch(function (error) {
            console.warn("Can't load entities or total for matches:", error);
            setMatches([]);
            setTotal(0);
        })
            .finally(function () {
            onFinishLoading === null || onFinishLoading === void 0 ? void 0 : onFinishLoading();
        });
    }, 
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [
        globalFilter,
        activeness,
        dtssPath,
        enabled,
        entityUri,
        field,
        filter,
        rules,
        markMatchedValues,
        getPagedMatchesForDataTenantEntity,
        getTransitiveMatchesWithPaging,
        onFinishLoading,
        onStartLoading,
        options,
        order,
        page,
        rowsPerPage,
        safePromise,
        tenant,
        showMasking
    ]);
    useEffect(function () {
        getMatches();
    }, [getMatches]);
    var loadMatches = useCallback(function () {
        getMatches(true);
    }, [getMatches]);
    return {
        total: total,
        matches: matches,
        entitiesMap: entitiesMap,
        loadMatches: loadMatches
    };
};
