import { computed, ref } from 'vue';
export function useCrudListState(initial) {
    const query = ref({ ...initial, sorts: [...initial.sorts], filters: [...initial.filters] });
    const selectedId = ref(null);
    const dirty = ref(false);
    function replace(next) { query.value = { ...next, sorts: [...next.sorts], filters: [...next.filters] }; }
    function setPage(page, pageSize = query.value.pageSize) { query.value = { ...query.value, page, pageSize }; }
    function setSearch(search) { query.value = { ...query.value, page: 1, search: search?.trim() || undefined }; }
    function reset() { replace(initial); selectedId.value = null; dirty.value = false; }
    return {
        query,
        selectedId,
        dirty,
        offset: computed(() => (query.value.page - 1) * query.value.pageSize),
        replace,
        setPage,
        setSearch,
        reset
    };
}
