import { documentHelpers, selectionHelpers, last } from 'substance'
import FigurePanel from './models/FigurePanel'

export function setContainerSelection (tx, node, surfaceId) {
  if (!surfaceId) surfaceId = node.id
  const p = node.find('p')
  if (p) {
    let path = [p.id, 'content']
    let newSelection = {
      type: 'property',
      path,
      startOffset: 0,
      surfaceId
    }
    tx.setSelection(newSelection)
  }
}

export function importFigures (tx, sel, files, paths) {
  if (files.length === 0) return

  let containerPath = sel.containerPath
  let figures = files.map((file, idx) => {
    let href = paths[idx]
    let mimeType = file.type
    let panelTemplate = FigurePanel.getTemplate()
    panelTemplate.content.href = href
    panelTemplate.content.mimeType = mimeType
    let figure = documentHelpers.createNodeFromJson(tx, {
      type: 'figure',
      panels: [ panelTemplate ]
    })
    // Note: this is necessary because tx.insertBlockNode()
    // selects the inserted node
    // TODO: maybe we should change the behavior of tx.insertBlockNode()
    // so that it is easier to insert multiple nodes in a row
    if (idx !== 0) {
      tx.break()
    }
    tx.insertBlockNode(figure)
    return figure
  })
  selectionHelpers.selectNode(tx, last(figures).id, containerPath)
}

@@ -113,325 +113,8 @@
 l'%0A%0A
-export function setContainerSelection (tx, node, surfaceId) %7B%0A  if (!surfaceId) surfaceId = node.id%0A  const p = node.find('p')%0A  if (p) %7B%0A    let path = %5Bp.id, 'content'%5D%0A    let newSelection = %7B%0A      type: 'property',%0A      path,%0A      startOffset: 0,%0A      surfaceId%0A    %7D%0A    tx.setSelection(newSelection)%0A  %7D%0A%7D%0A%0A
 expo

Remove obsolete code.