import { pipe, values, filter, map, mapValues, pick, isNullish } from 'remeda';
export const useTodoStore = create<TodoStoreState & TodoStoreAction>()(
  devtools((set, get) => ({
    
    buildUpsertTodoRequestDto: ({ vendorId, userId }) => {
      const todosObj = get().todos;
      const todos = pipe(
        
        todosObj as Record<string, TodoItem>,
        
        values, 
        
        filter((t) => {
          const isChanged = get().getIsChangedTodo(t.id);
          const isEditable = t?.ownerId ? t.ownerId === userId : true;
          return isChanged && isEditable;
        }),
        
        map((t) => {
          const id = t.id;
          return {
            ...t,
            
            todoStatus: getTodoStatus(get().getTodoById(id, true), t),
          };
        }),
        
        map(
          mapValues((v) => (isNullish(v) || v === '' ? null : v))
        ),
        
        map(
          pick(['id', 'title', 'note', 'completed', 'isPrimary']) 
        )
      );
      return { vendorId, todos };
    },
  }))
);