"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useReadableSearchState = void 0;
var react_1 = require("react");
var ramda_1 = require("ramda");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var MdmModuleContext_1 = require("../../contexts/MdmModuleContext");
var useReadableSearchState = function (searchState) {
    var lookupsListResolved = (0, MdmModuleContext_1.useMdmAction)('lookupsListResolved');
    var metadata = (0, MdmModuleContext_1.useMdmMetadata)();
    var dataTenants = (0, MdmModuleContext_1.useMdmDataTenants)();
    var attributesPresentation = (0, MdmModuleContext_1.useMdmAttributesPresentation)();
    var lookups = (0, MdmModuleContext_1.useMdmLookups)();
    var map = searchState.map;
    var keyword = (0, mdm_sdk_1.getKeywordFromSearchState)(searchState);
    var groups = (0, mdm_sdk_1.getGroupsFromSearchState)(metadata, dataTenants, true, searchState);
    var unresolvedLookups = (0, mdm_sdk_1.getUnresolvedLookupsListFromSearchGroups)(groups, lookups);
    (0, react_1.useEffect)(function () {
        if (unresolvedLookups.length > 0) {
            (0, ramda_1.pipe)((0, ramda_1.chain)(function (_a) {
                var type = _a.type, codeValues = _a.codeValues;
                return codeValues.map(function (codeValue) { return ({ type: type, codeValue: codeValue }); });
            }), mdm_sdk_1.resolveLookupsList, (0, ramda_1.andThen)(lookupsListResolved))(unresolvedLookups);
        }
    }, [searchState]); // eslint-disable-line
    return {
        readableQuery: (0, mdm_sdk_1.buildReadableQuery)(metadata, attributesPresentation, lookups, groups, keyword, map),
        readableQueryString: (0, mdm_sdk_1.buildReadableQueryToString)(metadata, attributesPresentation, lookups, groups, keyword, map)
    };
};
exports.useReadableSearchState = useReadableSearchState;
