import { canLayerAcceptChildren } from '../store/layer-utils.mjs';
import { hasChildrenFieldOfTypeString } from '../store/schema-utils.mjs';

function canPasteLayer(sourceLayer, targetLayerId, componentRegistry, findLayerById) {
  const targetLayer = findLayerById(targetLayerId);
  if (!targetLayer) {
    return false;
  }
  if (!canLayerAcceptChildren(targetLayer, componentRegistry)) {
    return false;
  }
  const targetDef = componentRegistry[targetLayer.type];
  if (targetDef && "shape" in targetDef.schema && hasChildrenFieldOfTypeString(targetDef.schema)) {
    return false;
  }
  const sourceDef = componentRegistry[sourceLayer.type];
  if (sourceDef?.childOf && !sourceDef.childOf.includes(targetLayer.type)) {
    return false;
  }
  return true;
}

export { canPasteLayer };
