import { v4 as uuidv4 } from 'uuid'
import AddIcon from '@mui/icons-material/Add'
import ClearIcon from '@mui/icons-material/Clear'
import { observer } from 'mobx-react'
import React, { useState } from 'react'
import {
MenuItem,
FormControl,
Button,
Select,
Input,
Checkbox,
ListItemText,
IconButton,
List,
ListItem,
Tooltip,
} from '@mui/material'
/**
* An element representing an individual filter with a category and set of
* applied values
*/
const Filter = observer((props: any) => {
const { schema, filterModel, facets } = props
const [categoryValue, setCategoryValue] = useState(
filterModel.category
? facets.find((f: { name: string }) => f.name === filterModel.category)
: facets[0],
)
const [filterValue, setFilterValue] = useState(
filterModel.filter ? filterModel.filter.split(',') : [],
)
/**
* Converts filter model objects to a GDC filter query and updates the track
* @param {*} filters Array of filter model objects
* @param {*} target Track target
*/
function updateTrack(
filters: { filter: string; type: string; category: string }[],
target: any,
) {
let gdcFilters: { op?: string; content?: any[] } = {
op: 'and',
content: [],
}
if (filters.length > 0) {
for (const filter of filters) {
if (filter.filter !== '') {
gdcFilters.content?.push({
op: 'in',
content: {
field: `${filter.type}s.${filter.category}`,
value: filter.filter.split(','),
},
})
}
}
} else {
gdcFilters = {}
}
target.adapter.filters.set(JSON.stringify(gdcFilters))
}
const handleFilterDelete = () => {
schema.deleteFilter(filterModel.id)
updateTrack(schema.filters, schema.target)
}
return (
<>
>
)
})
/**
* A collection of filters along with a button to add new filters
*/
const FilterList = observer(({ schema, type, facets }: Record) => {
const initialFilterSelection = facets[0].name
const handleClick = () => {
schema.addFilter(uuidv4(), initialFilterSelection, type, '')
}
return (
<>
{schema.filters.map((filterModel: any) => {
if (filterModel.type === type) {
return (
)
}
return null
})}
}>
Add Filter
>
)
})
export default FilterList