import { ArrayValue, BooleanValue, NumberValue, RecordValue, StringValue, } from '@coveo/bueno';
import { createAction } from '@reduxjs/toolkit';
import { requiredNonEmptyString, serializeSchemaValidationError, validatePayload, validatePayloadAndThrow, } from '../../../../utils/validate-payload.js';
import { validateManualNumericRanges } from '../../../facets/range-facets/numeric-facet-set/numeric-facet-actions.js';
export const toggleSelectNumericFacetValue = createAction('commerce/facets/numericFacet/toggleSelectValue', (payload) => validatePayload(payload, {
    facetId: requiredNonEmptyString,
    selection: new RecordValue({
        values: numericFacetValueDefinition,
    }),
}));
export const toggleExcludeNumericFacetValue = createAction('commerce/facets/numericFacet/toggleExcludeValue', (payload) => validatePayload(payload, {
    facetId: requiredNonEmptyString,
    selection: new RecordValue({
        values: numericFacetValueDefinition,
    }),
}));
export const updateNumericFacetValues = createAction('commerce/facets/numericFacet/updateValues', (payload) => {
    try {
        validatePayloadAndThrow(payload, {
            facetId: requiredNonEmptyString,
            values: new ArrayValue({
                each: new RecordValue({ values: numericFacetValueDefinition }),
            }),
        });
        validateManualNumericRanges({ currentValues: payload.values });
        return { payload, error: null };
    }
    catch (error) {
        return { payload, error: serializeSchemaValidationError(error) };
    }
});
export const updateManualNumericFacetRange = createAction('commerce/facets/numericFacet/updateManualRange', (payload) => validatePayloadAndThrow(payload, {
    facetId: requiredNonEmptyString,
    ...numericFacetValueDefinition,
}));
const numericFacetValueDefinition = {
    state: new StringValue({
        required: true,
        constrainTo: ['idle', 'selected', 'excluded'],
    }),
    start: new NumberValue({ required: true }),
    end: new NumberValue({ required: true }),
    endInclusive: new BooleanValue({ required: true }),
};
