'use client';
import React, { createContext, useContext, ReactNode, useMemo } from 'react';
import { BuilderContextProps } from '@/src/types/builder';

const defaultContext: BuilderContextProps = {
  onElementSelect: () => {},
  onElementRemove: () => {},
  onElementMove: () => {},
  onElementUpdate: () => {},
  onAddComponent: () => {},
  onAddToSection: () => {},
  selectedElementId: null,
  isPreview: false,
  portfolioProjects: [],
};

const BuilderContext = createContext<BuilderContextProps>(defaultContext);

export function BuilderProvider({
  children,
  value = {},
}: {
  children: ReactNode;
  value?: Partial<BuilderContextProps>;
}) {
  const contextValue = useMemo(() => ({ ...defaultContext, ...value }), [value]);
  return <BuilderContext.Provider value={contextValue}>{children}</BuilderContext.Provider>;
}

export function useBuilder() {
  return useContext(BuilderContext);
}
