import { Switch } from "@/components/ui/switch";
import { useToast } from "@/components/ui/use-toast";
import { useRouter } from "@/i18n/routing";
import { setStaticBanner } from "@/service/settings/settings";

export default function StaticToogle(props: {
  id: number;
  initChecked: boolean;
}) {
  const { id, initChecked } = props;
  const { toast } = useToast();
  const router = useRouter();

  const changeStaticBanner = async (id: number) => {
    const response = await setStaticBanner(id);

    if (response?.error) {
      toast({
        variant: "destructive",
        title: response?.message,
      });
      return false;
    }

    toast({
      title: "Success ",
    });
    router.push("/admin/settings/banner?dataChange=true");
  };

  return (
    <Switch
      id="static-toogle"
      checked={initChecked}
      onCheckedChange={(e) => {
        changeStaticBanner(id);
      }}
    />
  );
}
