import {div, header, footer, button, p, hJSX} from '@cycle/dom'
import isolate from '@cycle/isolate'
import AceEditor from 'cyclejs-ace-editor'

let {Observable} = require('rx')

function intent({DOM}) {
    const buttonClicks$ = DOM.select('.submit-button').events('click')

    return {
        buttonClicks$
    }
}

function view(subjectCodeEditor) {

    return Observable
        .just(
            <div id="code">
                <header>
                    <div className="container-header">
                        Your code
                    </div>
                </header>
                <div className="code">
                    { subjectCodeEditor.DOM }
                </div>
                <footer>
                    <button className="submit-button">Submit</button>
                </footer>
            </div>
        )
}

function CodePanel(sources) {
    const codeTemplate = 'function sum() {\n\n}\n\nmodule.exports = sum'

    const {DOM} = sources
    const initialValue$ = Observable.just(codeTemplate)

    const {buttonClicks$} = intent(sources)

    const params$ = Observable.just({
      theme: 'ace/theme/monokai',
      mode: 'ace/mode/javascript'
    })

    const subjectCodeEditor = AceEditor({DOM, initialValue$, params$})
    const vtree$ = view(subjectCodeEditor)

    return {
        DOM: vtree$,
        buttonClicks$: buttonClicks$,
        code$: subjectCodeEditor.value$
    }
}

function CodePanelWrapper(sources) {
    return isolate(CodePanel)(sources)
}

export default CodePanelWrapper

@@ -192,16 +192,25 @@
 ent(%7BDOM
+, context
 %7D) %7B%0A   
@@ -278,16 +278,112 @@
 lick')%0A%0A
+    const initialCodeValue$ = context.map(json =%3E %7B%0A      return json.initialCode %7C%7C ''%0A    %7D)%0A%0A
     retu
@@ -408,16 +408,43 @@
 nClicks$
+,%0A        initialCodeValue$
 %0A    %7D%0A%7D
@@ -1018,153 +1018,23 @@
 nst 
-codeTemplate = 'function sum() %7B%5Cn%5Cn%7D%5Cn%5Cnmodule.exports = sum'%0A%0A    const %7BDOM%7D = sources%0A    const initialValue$ = Observable.just(codeTemplate)
+%7BDOM%7D = sources
 %0A%0A  
@@ -1055,16 +1055,35 @@
 nClicks$
+, initialCodeValue$
 %7D = inte
@@ -1268,16 +1268,35 @@
 alValue$
+: initialCodeValue$
 , params

Use initial value from api