import {
  Button,
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
} from "@/ui";

import { DataTableProvider, useMediaQuery } from "@/lib/hooks";
import { t } from "i18next";
import { CatalogPickerDataTable } from "../CatalogPickerDataTable";

export const CatalogPickerDialog = ({
  isOpen,
  onOpenChange,
  onSelect,
}: {
  isOpen: boolean;
  onOpenChange: (open: boolean) => void;
  onSelect: (data: unknown) => void;
}) => {
  const isDesktop = useMediaQuery("(min-width: 768px)");

  if (isDesktop) {
    return (
      <Dialog modal open={isOpen} onOpenChange={onOpenChange}>
        <DialogContent className='w-full max-w-full md:w-9/12 lg:w-10/12'>
          <DialogHeader>
            <DialogTitle>{t("quotes.catalog_picker_dialog.title")}</DialogTitle>
            <DialogDescription>{t("quotes.catalog_picker_dialog.description")}</DialogDescription>
          </DialogHeader>
          <DataTableProvider syncWithLocation={false} initialPageSize={5}>
            <CatalogPickerDataTable onSelect={onSelect} />
          </DataTableProvider>

          <DialogFooter>
            <Button type='submit' onClick={() => onOpenChange(false)}>
              {t("common.close")}
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    );
  }

  return (
    <Drawer open={isOpen} onOpenChange={onOpenChange}>
      <DrawerContent>
        <DrawerHeader className='text-left'>
          <DrawerTitle>{t("quotes.catalog_picker_dialog.title")}</DrawerTitle>
          <DrawerDescription>{t("quotes.catalog_picker_dialog.description")}</DrawerDescription>
        </DrawerHeader>
        <DataTableProvider syncWithLocation={false} initialPageSize={5}>
          <CatalogPickerDataTable onSelect={onSelect} />
        </DataTableProvider>
        <DrawerFooter className='pt-2'>
          <DrawerClose asChild>
            <Button variant='outline'>{t("common.close")}</Button>
          </DrawerClose>
        </DrawerFooter>
      </DrawerContent>
    </Drawer>
  );
};
