import { WORKSPACE_ID } from '../../constants';
import cloneComponentAndChildren from './utilities';
import updateStyle from './updateStyle';
import UPDATE_STYLE from '../../actions/workspace';

/**
 * Moves a component already in the workspace
 * @param {object} state
 * @param {object} action
 * -> @property {number} action.targetID - ID of parent component to add component to
 * -> @property {number} action.sourceID - ID of component being moved into another
 */
export default function moveChild(state, action) {
  if (action.targetID === action.sourceID) {
    return state;
  }

  const components = { ...state.components };
  const source = cloneComponentAndChildren(components[action.sourceID]);
  const target = cloneComponentAndChildren(components[action.targetID]);
  const prevParent = cloneComponentAndChildren(components[source.parentID]);
  [source, target, prevParent].forEach(component => components[component.id] = component);

  if (source.parentID === target.id || target.parentID === source.id) {
    return state;
  }

  source.parentID = target.id;
  target.children.push(source.id);
  prevParent.children = prevParent.children.filter(val => val !== source.id);

  const [sourceWidth, sourceHeight, targetWidth, targetHeight] = [
    source.props.style.width,
    source.props.style.height,
    target.props.style.width,
    target.props.style.height
  ].map(elem => parseInt(elem.split('px')[0]));


  const reduceSourceSize = sourceHeight > targetHeight || sourceWidth > targetWidth;
  const newStyle = { left: 0, top: 0 };

  if (reduceSourceSize) {
    newStyle.width = `${targetWidth / 2}px`;
    newStyle.height = `${targetHeight / 2}px`;
  }

  const updateStyleAction = {
    type: UPDATE_STYLE,
    sourceID: source.id,
    newStyle
  };

  const newState = updateStyle({ ...state, components }, updateStyleAction);

  return newState;
}

@@ -186,16 +186,79 @@
 kspace';
+%0Aimport %7B pixelsToInt %7D from '../../utilities/helperFunctions';
 %0A%0A/**%0A *
@@ -1455,20 +1455,23 @@
 =%3E p
-arse
+ixelsTo
 Int(elem
 .spl
@@ -1470,23 +1470,8 @@
 elem
-.split('px')%5B0%5D
 ));%0A

Integrate pixelsToInt helper function into moveChild reducer