import { PortfolioConfig, PortfolioElement } from '@/src/types/portfolio';
import { ComponentType } from '@/src/components/builder/config/componentRegistry';
import { ElementService } from './ElementService';

export class BuilderService {
  static addComponent(
    index: number,
    componentType: ComponentType,
    config?: PortfolioConfig,
    initialProps?: Partial<PortfolioElement['props']>
  ): PortfolioConfig {
    const newElements = ElementService.addElementAtIndex(index, componentType, config?.elements, initialProps);
    return { ...config, elements: newElements };
  }

  static addToSection(
    config: PortfolioConfig,
    sectionId: string,
    position: string,
    componentType: ComponentType,
    initialProps?: Partial<PortfolioElement['props']>,
    top?: boolean
  ): PortfolioConfig {
    const newElement = ElementService.createElement(componentType, undefined, position, initialProps);
    const newElements = ElementService.addToSection(config.elements, sectionId, newElement, top);
    return { ...config, elements: newElements };
  }

  static updateElementProps(
    config: PortfolioConfig,
    elementId: string,
    newProps: PortfolioElement['props']
  ): PortfolioConfig {
    const newElements = ElementService.updateProps(config.elements, elementId, newProps);
    return { ...config, elements: newElements };
  }

  static removeElement(config: PortfolioConfig, elementId: string): PortfolioConfig {
    const newElements = ElementService.removeElement(config.elements, elementId);
    return { ...config, elements: newElements };
  }

  static moveElement(config: PortfolioConfig, elementId: string, direction: 'up' | 'down'): PortfolioConfig {
    const newElements = ElementService.moveElement(config.elements, elementId, direction);
    return { ...config, elements: newElements };
  }
}
