"use client";
import { Switch } from "@/components/ui/switch";
import { error } from "@/config/swal";
import { useRouter } from "@/i18n/routing";
import { publishUnpublishCategory } from "@/service/settings/settings";

export default function StatusToogle(props: {
  id: number;
  initValue: boolean;
}) {
  const { id, initValue } = props;
  const router = useRouter();
  const publishCategory = async (id: number, status: string) => {
    const response = await publishUnpublishCategory(id, status);
    console.log(response);
    if (response?.error) {
      error(response.message);
      return false;
    }
    router.push("/admin/settings/category?dataChange=true");
  };
  return (
    <Switch
      id={String(id)}
      checked={initValue}
      onCheckedChange={(e) => publishCategory(id, String(e))}
    />
  );
}
