import { useGetIdentity, useListFilterContext, useTranslate } from "ra-core";
import { Label } from "@/components/ds/ui/label";
import { Switch } from "@/components/ds/ui/switch";

export const OnlyMineInput = (_: { alwaysOn: boolean; source: string }) => {
  const { filterValues, displayedFilters, setFilters } = useListFilterContext();
  const { identity } = useGetIdentity();
  const translate = useTranslate();

  const handleChange = () => {
    const newFilterValues = { ...filterValues };
    if (typeof filterValues.sales_id !== "undefined") {
      delete newFilterValues.sales_id;
    } else {
      newFilterValues.sales_id = identity && identity?.id;
    }
    setFilters(newFilterValues, displayedFilters);
  };
  return (
    <div className="mt-auto pb-2.25">
      <div className="flex items-center space-x-2">
        <Switch
          id="only-mine"
          checked={typeof filterValues.sales_id !== "undefined"}
          onCheckedChange={handleChange}
        />
        <Label htmlFor="only-mine">
          {translate("crm.deal.filter.only_companies_i_manage")}
        </Label>
      </div>
    </div>
  );
};
