import { debounce } from "lodash";
import { UIStore } from "ui/domain/uiStore";

type SearchUIStore = Pick<UIStore, "query"| "changeQuery" | "executeQuery">

const deboucedTask = debounce((task) => Promise.resolve(task()), 500);

const changeSearchQueryUseCase = (store: SearchUIStore, payload: string) => {
  if(store.query !== payload) {
    store.changeQuery(payload);
    return deboucedTask(() => store.executeQuery(payload));
  }
};

const resetSearchQueryUseCase = (store: Omit<SearchUIStore, "executeQuery">) => {
  store.changeQuery('');
}

export {changeSearchQueryUseCase, resetSearchQueryUseCase};