import { computed, ref, watch, type Ref } from 'vue' import type { DataOptions } from './types' export function useTableProps({ componentAttributes, serverItemsLength, options, storedOptions, }: { componentAttributes: Record serverItemsLength?: number options: Ref> storedOptions?: Partial }): { propsFacade: Ref> updateOptions: (tableOptions: Partial) => void } { const localOptions = ref>(storedOptions || options.value || {}) const propsFacade = computed(() => { // eslint-disable-next-line @typescript-eslint/no-unused-vars const { 'onUpdate:options': _, ...attrs } = componentAttributes const props = { ...attrs, ...localOptions.value, ...(serverItemsLength !== undefined ? { itemsLength: serverItemsLength } : {}), } return props }) // When the table options are updated, merge them into localOptions function updateOptions(tableOptions: Partial): void { const merged: Partial = { ...options.value, ...tableOptions, } // Shallow equality check to prevent redundant reassignments (which would emit twice) const prev = options.value || {} let changed = false const keys = new Set([ ...Object.keys(prev as Record), ...Object.keys(merged as Record), ]) for (const key of keys) { if ((prev as Record)[key] !== (merged as Record)[key]) { changed = true break } } if (changed) { options.value = merged } } // Watch for external changes to options and update localOptions accordingly watch(options, (newOptions) => { localOptions.value = { ...localOptions.value, ...newOptions, } }) return { propsFacade, updateOptions, } }