import React, { useCallback } from 'react';
import { getAttributesListForReadMode, getAttributeTypeSubAttributes } from '@reltio/mdm-sdk';
import { ArrowExpandButton } from '../../../../../ArrowExpandButton';
import { BranchDecorator } from '../../../../../BranchDecorator';
import { useSyncedExpandedAttributes } from '../../../../../contexts/SyncedExpandedAttributesContext';
import { useMdmMetadata } from '../../../../../contexts/MdmModuleContext';
import { SimpleRule } from '../SimpleRule';
import { AttributeRule } from '../AttributeRule';
import styles from './ComplexRule.module.css';
export var ComplexRule = function (_a) {
    var attributeType = _a.attributeType, attributeValue = _a.attributeValue, getRuleTypeData = _a.getRuleTypeData;
    var metadata = useMdmMetadata();
    var expandedContext = useSyncedExpandedAttributes();
    var attributeUri = attributeValue.uri;
    var expanded = expandedContext ? expandedContext.isExpanded(attributeUri) : false;
    var toggle = useCallback(function () {
        if (expandedContext && attributeUri) {
            expandedContext.toggle(attributeUri);
        }
    }, [expandedContext, attributeUri]);
    var subAttributes = getAttributeTypeSubAttributes(metadata, attributeType);
    var entity = { attributes: attributeValue.value };
    var attributesList = getAttributesListForReadMode(subAttributes, entity, false, []);
    return (React.createElement("div", null,
        React.createElement("div", { className: styles.expandableRule },
            React.createElement(ArrowExpandButton, { expanded: expanded, onClick: toggle }),
            React.createElement(SimpleRule, { attributeValue: attributeValue, attributeType: attributeType, getRuleTypeData: getRuleTypeData })),
        expanded && (React.createElement("div", null, attributesList.map(function (item) { return (React.createElement(BranchDecorator, { key: item.attrType.uri, enabled: true, className: styles.rulePaddingWrapper },
            React.createElement(AttributeRule, { attributeType: item.attrType, values: item.values, getRuleTypeData: getRuleTypeData, parentUri: attributeUri }))); })))));
};
