import React, {FC, ReactNode} from "react"; import {ComponentAction, ComponentRuntimeDataWithParentType, getDefaultRuntimeComponentData} from "./tree/components"; import {Tabs} from "./tree/fields/tabs"; import {__} from "./globals"; import {ComponentsMeta, getComponentMeta, getComponentMetaData} from "./tree/ComponentsMeta"; import {QuantityRenderer, QuantityMeta} from "./tree/cards/Quantity"; import classNames from "classnames"; import {ProductsRenderer} from "./tree/cards/ProductsRenderer"; import {NULL} from "sass"; import {QuantityDecision} from "./TestablesPopupWindow"; import {getCardRendererFromType} from "./tree/cards"; import {TestableData} from "./tree/store"; import {withDecodedEntities} from "./helpers"; export type ExtraTestableFieldsProps = { componentRuntimeData: ComponentRuntimeDataWithParentType[], onSetComponents: (actions: { add?: ComponentRuntimeDataWithParentType[], remove?: ComponentRuntimeDataWithParentType[] }) => void, currentFilterId: string | null, fields: () => ReactNode, testableType: TestableData['testableType'] } const getDefaultRuntimeComponentDataWithParentType = (type: string): ComponentRuntimeDataWithParentType => ({ parentType: 'filters', ...getDefaultRuntimeComponentData(getComponentMeta('filters', type))!, } ) const quantityDefaultRuntimeData: ComponentRuntimeDataWithParentType = getDefaultRuntimeComponentDataWithParentType(QuantityRenderer.type!) const getExtraComponentsAvailable = (componentRuntimeData: ComponentRuntimeDataWithParentType[], currentFilterId, testableType: TestableData['testableType']): { type: string, name: string, prename: string, action: string }[] => { const filters = ComponentsMeta[`${testableType}s`] const allRegisteredFiltersExceptSelected = filters.filter(({id}) => { return !componentRuntimeData.find((selectedComponentData) => { const {type} = selectedComponentData const selected = type === id let canShowAsAvailableAfterBeingSelected = false if (selected) { const renderer = getCardRendererFromType(type) const extraCheck = renderer.canShowAsAvailableWhileBeingAmongstSelected?.(selectedComponentData.options, currentFilterId) if (typeof extraCheck === 'boolean') { canShowAsAvailableAfterBeingSelected = extraCheck } } // needs to be ==== false, don't change it. return selected && canShowAsAvailableAfterBeingSelected === false }) }) const filtersCompatibleWithCurrentSelection = allRegisteredFiltersExceptSelected.filter(componentMeta => { // get the selected filters //loop though each of them and check that the componentMeta is not present in the incompatibleWith array of the selected filter const selectedFilters = componentRuntimeData; // not redundant, its more readable for (const selectedFilter of selectedFilters) { const selectedFilterMeta = getComponentMeta(selectedFilter.parentType, selectedFilter.type); const incompatibleWith = selectedFilterMeta?.incompatibleWith || []; // if the componentMeta is not in the incompatibleWith array, then it is compatible if (incompatibleWith.includes(componentMeta.id)) { return false; } } return true }) return filtersCompatibleWithCurrentSelection.map(componentMeta => ({ type: componentMeta.id, name: withDecodedEntities(getComponentMetaData(componentMeta)).name, prename: componentMeta?.prename?.(componentRuntimeData) || 'Any', action: componentMeta?.action?.(componentRuntimeData) === ComponentAction.Exclude ? 'Exclude' : 'Select', })) /*[ { name: getComponentMetaData(['filters', Products.type!]).name, prename: getComponentMeta('filters', Products.type!)?.prename?.(componentRuntimeData) || 'Any', action: getComponentMeta('filters', Products.type!)?.action?.(componentRuntimeData) === ComponentAction.Exclude ? 'Exclude' : 'Select', }/ * { prename: 'No excluded', name: 'Products', action: 'Exclude Products' }, { prename: 'With any', name: 'Attributes', action: 'Select Attributes' }, { prename: 'Any', name: 'Brands', action: 'Select Brands' }* /];*/ } export const ExtraTestableFields: FC = ({ componentRuntimeData, onSetComponents, currentFilterId, fields, testableType }) => { const extraComponentsAvailable = getExtraComponentsAvailable(componentRuntimeData, currentFilterId, testableType); return
{false &&
{fields()}
} {!!extraComponentsAvailable.length &&

{__('Narrow down product selection') || __('Further filter products') || __('Combine with other filters')}({__('Optional')})

{__('Products must match every selected filter to be eligible') || __('Products must match all selected filters')}

{extraComponentsAvailable.map(({ type, name, prename, action }) =>
)}
} ; }