import { AllNonCompoundPredicateOperators, PredicateCompoundOperator, PredicateOperator } from '@standardnotes/snjs'
import { observer } from 'mobx-react-lite'
import Button from '../Button/Button'
import Icon from '../Icon/Icon'
import { CompoundPredicateBuilderController } from './CompoundPredicateBuilderController'
import { PredicateKeypath, PredicateKeypathLabels, PredicateKeypathTypes } from './PredicateKeypaths'
import PredicateValue from './PredicateValue'
import { c } from 'ttag'
type Props = {
controller: CompoundPredicateBuilderController
}
const CompoundPredicateBuilder = ({ controller }: Props) => {
const { operator, setOperator, predicates, setPredicate, changePredicateKeypath, addPredicate, removePredicate } =
controller
return (
<>
{
setOperator(event.target.value as PredicateCompoundOperator)
}}
/>
{c('B4.Notes.TagsLinkedItems.Label').t`Should match ALL conditions`}
{
setOperator(event.target.value as PredicateCompoundOperator)
}}
/>
{c('B4.Notes.TagsLinkedItems.Label').t`Should match ANY conditions`}
{predicates.map((predicate, index) => (
{index !== 0 && (
{operator === 'and'
? c('B4.Notes.TagsLinkedItems.Label').t`AND`
: c('B4.Notes.TagsLinkedItems.Label').t`OR`}
)}
{
changePredicateKeypath(index, event.target.value as PredicateKeypath)
}}
>
{Object.entries(PredicateKeypathLabels).map(([key, label]) => (
{label}
))}
{PredicateKeypathTypes[predicate.keypath as PredicateKeypath] === 'tag' ? (
include
) : (
{
setPredicate(index, { operator: event.target.value as PredicateOperator })
}}
>
{AllNonCompoundPredicateOperators.map((operator) => (
{operator}
))}
)}
{predicate.keypath && (
{
setPredicate(index, { value })
}}
/>
)}
{index !== 0 && (
{
removePredicate(index)
}}
>
)}
{index === predicates.length - 1 && (
{
addPredicate()
}}
>
{c('B4.Notes.TagsLinkedItems.Action').t`Add another condition`}
)}
))}
{predicates.some((predicate) => PredicateKeypathTypes[predicate.keypath as PredicateKeypath] === 'date') && (
{c('B4.Notes.TagsLinkedItems.Label').t`Date Examples:`}
{c('B4.Notes.TagsLinkedItems.Info').t`To get all the items modified within the last 7 days, you can use`}{' '}
{c('B4.Notes.TagsLinkedItems.Label').t`User Modified Date`} >{' '}
7.days.ago
{c('B4.Notes.TagsLinkedItems.Info').t`To get all the items created before June 2022, you can use`}{' '}
{c('B4.Notes.TagsLinkedItems.Action').t`Created At`} <{' '}
06/01/2022
)}
>
)
}
export default observer(CompoundPredicateBuilder)