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 { useEffect, useMemo } from 'react';
import { flatten, pipe, pluck, uniq, without, equals } from 'ramda';
import { getRuleBasedAttributes, isEditableMode, parseRuleBasedAttribute } from '@reltio/mdm-sdk';
import { useChangedAttributes } from '../useChangedAttributes';
import { findRulesToCheck, findShowAttributeURIs } from './helpers';
import { useMdmHiddenAttributes, useMdmAction, useMdmEntity, useMdmMetadata, useMdmMode, useMdmModifiedEntity } from '../../contexts/MdmModuleContext';
export var useHiddenAttributes = function () {
    var metadata = useMdmMetadata();
    var mdmEntity = useMdmEntity();
    var mode = useMdmMode();
    var modifiedEntity = useMdmModifiedEntity(mdmEntity === null || mdmEntity === void 0 ? void 0 : mdmEntity.uri);
    var entity = isEditableMode(mode) && modifiedEntity ? modifiedEntity : mdmEntity;
    var entityUri = entity === null || entity === void 0 ? void 0 : entity.uri;
    var updateHiddenAttributes = useMdmAction('updateHiddenAttributes');
    var resetHiddenAttributes = useMdmAction('resetHiddenAttributes');
    var hiddenAttributes = useMdmHiddenAttributes();
    var currentEntityHiddenAttributes = useMemo(function () { return hiddenAttributes[entityUri] || null; }, [hiddenAttributes, entityUri]);
    var updateControlAttributes = useMdmAction('updateControlAttributes');
    var ruleBasedAttributes = useMemo(function () { return getRuleBasedAttributes(metadata, entity === null || entity === void 0 ? void 0 : entity.type); }, [metadata, entity === null || entity === void 0 ? void 0 : entity.type]);
    var parsedRuleBasedAttributes = useMemo(function () {
        return ruleBasedAttributes.map(function (rule) {
            var _a;
            return (__assign(__assign({}, parseRuleBasedAttribute(rule)), { showAttributeURIs: ((_a = rule === null || rule === void 0 ? void 0 : rule.controlFunction) === null || _a === void 0 ? void 0 : _a.showAttributeURI) || [] }));
        });
    }, [ruleBasedAttributes]);
    var newControlAttributes = useMemo(function () { return pipe(pluck('controlAttributes'), flatten, uniq)(parsedRuleBasedAttributes); }, [parsedRuleBasedAttributes]);
    var changedAttributes = useChangedAttributes(entityUri, entity === null || entity === void 0 ? void 0 : entity.attributes, newControlAttributes);
    var showAttributeURIs = useMemo(function () {
        return changedAttributes.length ? findShowAttributeURIs(changedAttributes, parsedRuleBasedAttributes) : [];
    }, [parsedRuleBasedAttributes, changedAttributes]);
    var rulesToCheck = useMemo(function () {
        return showAttributeURIs.length ? findRulesToCheck(showAttributeURIs, parsedRuleBasedAttributes) : [];
    }, [parsedRuleBasedAttributes, showAttributeURIs]);
    var addedHiddenAttributes = useMemo(function () {
        return rulesToCheck.length
            ? rulesToCheck.reduce(function (hiddenAttributes, _a) {
                var checkFn = _a.checkFn, showAttributeURIs = _a.showAttributeURIs;
                return checkFn(entity) ? without(showAttributeURIs, hiddenAttributes) : hiddenAttributes;
            }, showAttributeURIs)
            : [];
    }, [rulesToCheck, entity, showAttributeURIs]);
    useEffect(function () {
        if (entityUri && (addedHiddenAttributes.length || showAttributeURIs.length)) {
            var updatedHiddenAttributes = without(showAttributeURIs, currentEntityHiddenAttributes || []).concat(addedHiddenAttributes);
            if (!equals(currentEntityHiddenAttributes, updatedHiddenAttributes)) {
                updateHiddenAttributes(entityUri, updatedHiddenAttributes);
            }
        }
    }, [addedHiddenAttributes, showAttributeURIs, entityUri]);
    useEffect(function () {
        updateControlAttributes(newControlAttributes);
    }, [newControlAttributes]);
    useEffect(function () { return function () { return resetHiddenAttributes(entityUri); }; }, [entityUri]);
};
