'use strict';

const layerUtils = require('../store/layer-utils.cjs');
const schemaUtils = require('../store/schema-utils.cjs');

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

exports.canPasteLayer = canPasteLayer;
