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