"use client";

import { useCallback } from "react";

import type { Layer, LayerProps } from "@/lib/studio-types";
import { mergeLayerProps } from "@/lib/studio-layer-props";
import { useStudioStore } from "@/lib/studio-store";

export function useLayerUpdater(layer: Layer) {
  const updateLayer = useStudioStore((state) => state.updateLayer);

  const update = useCallback(
    (updates: Partial<Layer>) => {
      updateLayer(layer.id, updates);
    },
    [layer.id, updateLayer]
  );

  const updateProps = useCallback(
    (updates: LayerProps) => {
      updateLayer(layer.id, {
        props: mergeLayerProps(layer.props, updates),
      });
    },
    [layer.id, layer.props, updateLayer]
  );

  return { update, updateProps };
}
