import { findChild } from '../util/domHelpers'
import { getLabel } from '../../shared/nodeHelpers'

export default class FigurePanelConverter {
  get type () { return 'figure-panel' }

  // ATTENTION: figure-panel is represented in JATS
  // instead there is the distinction between fig-group and fig
  // which are represented as Figure in Texture
  get tagName () { return 'fig' }

  import (el, node, importer) {
    let $$ = el.createElement.bind(el.getOwnerDocument())
    let labelEl = findChild(el, 'label')
    let contentEl = this._getContent(el)
    let permissionsEl = findChild(el, 'permissions')
    let captionEl = findChild(el, 'caption')
    let doc = importer.getDocument()
    // Preparations
    if (!captionEl) {
      captionEl = $$('caption')
    }
    let titleEl = findChild(captionEl, 'title')
    if (!titleEl) {
      titleEl = $$('title')
    }
    // drop everything than 'p' from caption
    let captionContent = captionEl.children
    for (let idx = captionContent.length - 1; idx >= 0; idx--) {
      let child = captionContent[idx]
      if (child.tagName !== 'p') {
        captionEl.removeAt(idx)
      }
    }
    // there must be at least one paragraph
    if (!captionEl.find('p')) {
      captionEl.append($$('p'))
    }
    // Conversion
    if (labelEl) {
      node.label = labelEl.text()
    }
    node.title = importer.annotatedText(titleEl, [node.id, 'title'])
    // content is optional
    // TODO: really?
    if (contentEl) {
      node.content = importer.convertElement(contentEl).id
    }
    node.caption = captionEl.children.map(child => importer.convertElement(child).id)
    if (permissionsEl) {
      node.permission = importer.convertElement(permissionsEl).id
    } else {
      node.permission = doc.create({ type: 'permission' }).id
    }
  }

  _getContent (el) {
    return findChild(el, 'graphic')
  }

  export (node, el, exporter) {
    let $$ = exporter.$$
    // ATTENTION: this helper retrieves the label from the state
    let label = getLabel(node)
    if (label) {
      el.append($$('label').text(label))
    }
    // Attention: <title> is part of the <caption>
    if (node.title || node.caption) {
      let content = node.resolve('caption')
      let captionEl = $$('caption')
      if (content.length > 0) {
        captionEl.append(
          content.map(p => exporter.convertNode(p))
        )
      }
      if (node.title) {
        // Note: this would happen if title is set, but no caption
        if (!captionEl) captionEl = $$('caption')
        captionEl.insertAt(0,
          $$('title').append(
            exporter.annotatedText([node.id, 'title'])
          )
        )
      }
      el.append(captionEl)
    }
    if (node.content) {
      el.append(
        exporter.convertNode(node.resolve('content'))
      )
    }
    let permission = node.resolve('permission')
    if (permission && !permission.isEmpty()) {
      el.append(
        exporter.convertNode(permission)
      )
    }
  }
}

@@ -1786,24 +1786,475 @@
  %7D).id%0A    %7D
+%0A%0A    // Custom Metadata Fields%0A    let kwdGroupEls = el.findAll('kwd-group')%0A    node.metadata = kwdGroupEls.map(kwdGroupEl =%3E %7B%0A      let kwdEls = kwdGroupEl.findAll('kwd')%0A      let labelEl = kwdGroupEl.find('label')%0A      let name = labelEl ? labelEl.textContent : ''%0A      let value = kwdEls.map(kwdEl =%3E kwdEl.textContent).join(', ')%0A      return doc.create(%7B%0A        type: 'custom-metadata-field',%0A        name,%0A        value%0A      %7D).id%0A    %7D)
 %0A  %7D%0A%0A  _get
@@ -3138,24 +3138,404 @@
 onEl)%0A    %7D%0A
+    // Custom Metadata Fields%0A    if (node.metadata.length %3E 0) %7B%0A      el.append(%0A        node.resolve('metadata').map(field =%3E %7B%0A          return $$('kwd-group').append(%0A            $$('label').text(field.name)%0A          ).append(%0A            field.name.split(',').map(str =%3E %7B%0A              return $$('kwd').text(str.trim())%0A            %7D)%0A          )%0A        %7D)%0A      )%0A    %7D%0A
     if (node

Implement converter for CustomMetadataFields