"use client";

import { createContext, useContext, useState, useCallback, ReactNode } from "react";

type EditModeContextType = {
  editMode: boolean;
  toggleEditMode: () => void;
  isDirty: boolean;
  setDirty: (v: boolean) => void;
};

const EditModeContext = createContext<EditModeContextType>({
  editMode: false,
  toggleEditMode: () => {},
  isDirty: false,
  setDirty: () => {},
});

export function EditModeProvider({ children }: { children: ReactNode }) {
  const [editMode, setEditMode] = useState(false);
  const [isDirty, setDirty] = useState(false);

  const toggleEditMode = useCallback(() => {
    setEditMode((prev) => !prev);
    setDirty(false);
  }, []);

  return (
    <EditModeContext.Provider value={{ editMode, toggleEditMode, isDirty, setDirty }}>
      {children}
    </EditModeContext.Provider>
  );
}

export function useEditMode() {
  return useContext(EditModeContext);
}
