"use client";
import * as React from "react";
import { CalendarIcon } from "@radix-ui/react-icons";
import { addDays, format } from "date-fns";
import { cn } from "../../../lib/utils";
import { Button } from "../../../components/ui/Button/button";
import { cva, type VariantProps } from "class-variance-authority";
import { Calendar } from "../../../components/ui/Calendar/calendar";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "../../../components/ui/Calendar/popover";


import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "../../../components/ui/select";

const datepickerpresetVariants = cva(
  "",
  {
    variants: {
      variant: {
        default:
          "w-[240px] justify-start text-left font-normal",
      },
      size: {
        default: "overflow-y-auto max-h-44",
        sm:"overflow-y-auto max-h-32",
        lg:"overflow-y-auto max-h-64"
      },
    },
    defaultVariants: {
      variant: "default",
      size:'default'
    },
  }
);


export function DatePickerWithPresets({
  className,
  variant,
  size,
}: React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof datepickerpresetVariants>) {
  const [date, setDate] = React.useState<Date>();

  return (
    <Popover>
      <PopoverTrigger asChild>
        <Button
          variant={"outline"}
          className={cn(datepickerpresetVariants({variant}),
            !date && "text-muted-foreground"
          )}
        >
          <CalendarIcon className="mr-2 h-4 w-4" />
          {date ? format(date, "PPP") : <span>Pick a date</span>}
        </Button>
      </PopoverTrigger>
      <PopoverContent
        align="start"
        className="flex w-auto flex-col space-y-2 p-2 bg-white"
      >
        <Select
          onValueChange={(value) =>
            setDate(addDays(new Date(), parseInt(value)))
          }
        >
          <SelectTrigger>
            <SelectValue placeholder="Select" />
          </SelectTrigger>
          <SelectContent position="popper" className={cn(datepickerpresetVariants({size }))}>
            <SelectItem value="-1">Yesterday</SelectItem>
            <SelectItem value="-2">2 Days ago</SelectItem>
            <SelectItem value="-7">Week ago</SelectItem>
            <SelectItem value="0">Today</SelectItem>
            <SelectItem value="2">In 2 Days</SelectItem>
            <SelectItem value="3">In 3 days</SelectItem>
            <SelectItem value="7">In a week</SelectItem>
          </SelectContent>
        </Select>
        <div className="rounded-md border">
          <Calendar mode="single" selected={date} onSelect={setDate} />
        </div>
      </PopoverContent>
    </Popover>
  );
}

export default {datepickerpresetVariants}