const render = require('../../render')
const test = require('tape')
// const parse = require('parse-element')
const s = require('brisky-struct')
// const strip = require('strip-formatting')

test('switch - basic', t => {
  const state = s({ field: { navigation: {} } })
  const app = render(
    {
      types: {
        switcher: {
          $: '$switch',
          $switch: (s, subs, tree, key) => {
            if (s.compute() > 1) {
              return subs.props[key].self
            }
          },
          text: { $: true }
        },
        spesh: {
          text: 'SPESH',
          $: 'root.items.$any',
          props: {
            default: { type: 'switcher' }
          }
        }
      },
      blax: { type: 'spesh' },
      blurf: { $: 'root.items.$any', props: { default: { type: 'element', text: '!!!' } } },
      bla: { type: 'spesh' },
      holder: {
        text: { $: 'navigation', $transform: val => '🦄 switcher! ' + val },
        switcher: {
          $switch: (state, subs, tree, key) => {
            if (state.compute() === 100) {
              return false
            } else if (state.compute() === 2) {
              return subs.props[key].any
            } else if (state.compute() !== 0) {
              return subs.props[key].self
            }
          },
          $: 'navigation.$switch',
          text: { $: true, $transform: val => val + ' SWITCH IT SELF' },
          props: {
            any: { type: 'spesh' }
          }
        },
        bla: {
          html: {
            $: 'navigation',
            $transform: val =>
            `<div style="background-color:#eeeeff;">---- UNDER <b>${val}</b> SWITCH ------</div>`
          }
        }
      }
    },
    state
  )

  state.set({
    items: [ 1, 2, 3, 4 ],
    navigation: [ '@', 'root', 'items', 0 ]
  })

  var cnt = 0
  const defer = val => new Promise(
    resolve => setTimeout(() => resolve(val), ++cnt * 500)
  )

  state.set(defer({ items: [ 100 ] }))
  state.set(defer({ items: [ 2 ] }))
  state.set(defer({ items: [ 0 ] }))
  state.set(defer({ items: state.items.map(val => 0) }))
  state.set(defer({ items: state.items.map((val, key) => key) }))
  state.set(defer({ items: [ 2 ] }))
  state.set(defer({ navigation: '🦄' }))
  state.set(defer({ navigation: 0 }))

  if (document.body) {
    document.body.appendChild(app)
  }

  t.end()
})

@@ -2356,8 +2356,1177 @@
 nd()%0A%7D)%0A
+%0A%0A/*%0Aglob tests%0A%0A'use strict'%0Aconst test = require('tape')%0Aconst subsTest = require('../util')%0A%0Atest('switch - glob', (t) =%3E %7B%0A  const subscription = %7B%0A    field: %7B%0A      $switch: %7B%0A        $remove: true,%0A        '*.b': %7B val: true %7D,%0A        'a.*.b': %7B val: true %7D%0A      %7D%0A    %7D%0A  %7D%0A%0A  const s = subsTest(%0A    t,%0A    %7B%0A      field: '$root.a.x.b',%0A      a: %7B%0A        x: %7B%0A          b: 'im a/x/b'%0A        %7D%0A      %7D,%0A      b: %7B%0A        b: 'im b/b'%0A      %7D,%0A      z: %7B%0A        b: 'im z/b',%0A        z: 'im z/z'%0A      %7D%0A    %7D,%0A    subscription%0A  )%0A%0A  s('initial subscription', %5B%0A    %7B type: 'new', path: 'a/x/b', tree: 'field/$switch/$current' %7D%0A  %5D)%0A%0A  s(%0A    'set field to b/b',%0A    %5B%0A      %7B path: 'a/x/b', type: 'remove', tree: 'field/$switch/$current' %7D,%0A      %7B path: 'b/b', type: 'new' %7D%0A    %5D,%0A    %7B field: '$root.b.b' %7D%0A  )%0A%0A  s(%0A    'set field to z/b',%0A    %5B%0A      %7B path: 'b/b', type: 'remove', tree: 'field/$switch/$current' %7D,%0A      %7B path: 'z/b', type: 'new' %7D%0A    %5D,%0A    %7B field: '$root.z.b' %7D%0A  )%0A%0A  s(%0A    'set field to z/z',%0A    %5B%0A      %7B path: 'z/b', type: 'remove', tree: 'field/$switch/$current' %7D%0A    %5D,%0A    %7B field: '$root.z.z' %7D%0A  )%0A%0A  t.end()%0A%7D)%0A*/%0A

add glob tests