// @ts-check
/**
* @import {FilterAction, treeAction} from './../../types.js'
*/
import { HEX2uuid } from '@commtool/sql-query'
import { addUpdateList } from '../../server.ws.js'
import { addFilter } from '../executeFilters/executeFilters.js'
import { errorLoggerUpdate } from '../../utils/requestLogger.js'
/**
* Handles filter-type add actions: visible, changeable, include, exclude, intersect.
*
* A filter object (`action.UIDObjectID`) is attached to a source (`action.UIDBelongsTo`).
* The filter targets either a dlist (include/exclude/intersect) or a user (visible/changeable).
* After applying the filter, the target list is flagged for WebSocket update.
*
* @param {treeAction} action
* @returns {Promise<void>}
*/
export const addFilterAction = async (action) => {
try {
await addFilter(
action.UIDObjectID,
action.UIDnewTarget,
/** @type {FilterAction} */ (action.Type),
HEX2uuid(action.UIDroot),
)
addUpdateList(action.UIDnewTarget)
} catch (e) {
errorLoggerUpdate(e || new Error('treeAdd.filterActions: Unknown error occurred'))
}
}