// 取得目前的params
import { computed } from "vue";
import { useRouter, useRoute } from "vue-router";

export default function useSearchParam() {
  const router = useRouter();
  const route = useRoute();

  const searchParams = computed(() => route.query);

  // 排除null

  const deleteNull = (query) => {
    let newQuery = {};
    for (let key in query) {
      if (Array.isArray(query[key]) && query[key].length === 0) continue;
      if (query[key]) {
        newQuery[key] = query[key];
      }
    }
    return newQuery;
  };

  const changeParams = (query) => {
    router.push({ path: route.path, query: deleteNull(query) });
  };

  return { searchParams, changeParams };
}
