import React, { PropTypes } from 'react'
import ReactDOM from 'react-dom'
import _ from 'lodash'

import {Key} from '../utils.js'

Prompt.propTypes = {
    options: PropTypes.array.isRequired,
    done: PropTypes.func.isRequired,
    prompt: PropTypes.string,
}

export default function Prompt({ prompt, done, options }) {
    const opts = []

    for (let i = 0; i < options.length; i++) {
        let opt = options[i]
        if (_.isString(opt)) {
            let str = opt
            while (str.length > 0 && keyTaken(str[0])) {
                str = str.substr(1)
            }
            opt = { text: opt, key: str[0] }
        }
        if (!opt.text || !opt.key || keyTaken(opt.key)) {
            throw 'invalid options'
        }
        opts.push(opt)
    }

    function keyTaken(k) {
        return _.map(opts, 'key').includes(k)
    }

    function onKeyDown(event) {
        event.stopPropagation()
        event.preventDefault()
        const key = opts.find(opt => Key[opt.key.toUpperCase()] === event.keyCode)
        if (!key && event.keyCode !== Key.ESC && event.keyCode !== Key.ENTER) {
            return
        }
        done(key.key || false)
    }

    return (
        <div tabIndex="0" onKeyDown={onKeyDown} className="prompt-dialog">
            <div className="prompt-content">
            {prompt || <strong>Select: </strong> }
            {opts.map(opt => {
                const idx = opt.text.indexOf(opt.key)
                function onClick(event) {
                    done(opt.key)
                    event.stopPropagation()
                }
                return (
                    <span key={opt.key} className="option" onClick={onClick}>
                        {idx !== -1 ? opt.text.substring(0, idx) : opt.text + '('}
                        <strong className="text-primary">{opt.key}</strong>
                        {idx !== -1 ? opt.text.substring(idx + 1) : ')'}
                    </span>
                )
            })}
            </div>
        </div>
    )
}
4bb58c1951f79e630'>diff</a><a href='/cgit/cloud-email/mitmproxy/stats/test/mitmproxy/test_flow_export/locust_get.py'>stats</a></td><td class='form'><form class='right' method='get' action='/cgit/cloud-email/mitmproxy/log/test/mitmproxy/test_flow_export/locust_get.py'>
<input type='hidden' name='id' value='5c45ca7f9aba05849bd8db24bb58c1951f79e630'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/cgit/cloud-email/mitmproxy/tree/?id=5c45ca7f9aba05849bd8db24bb58c1951f79e630'>root</a>/<a href='/cgit/cloud-email/mitmproxy/tree/test?id=5c45ca7f9aba05849bd8db24bb58c1951f79e630'>test</a>/<a href='/cgit/cloud-email/mitmproxy/tree/test/mitmproxy?id=5c45ca7f9aba05849bd8db24bb58c1951f79e630'>mitmproxy</a>/<a href='/cgit/cloud-email/mitmproxy/tree/test/mitmproxy/test_flow_export?id=5c45ca7f9aba05849bd8db24bb58c1951f79e630'>test_flow_export</a>/<a href='/cgit/cloud-email/mitmproxy/tree/test/mitmproxy/test_flow_export/locust_get.py?id=5c45ca7f9aba05849bd8db24bb58c1951f79e630'>locust_get.py</a></div><div class='content'>blob: 632d5d53727bd06ce66ec98bb87aebe3317e8e05 (<a href='/cgit/cloud-email/mitmproxy/plain/test/mitmproxy/test_flow_export/locust_get.py?id=5c45ca7f9aba05849bd8db24bb58c1951f79e630'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
<a id='n15' href='#n15'>15</a>
<a id='n16' href='#n16'>16</a>
<a id='n17' href='#n17'>17</a>
<a id='n18' href='#n18'>18</a>
<a id='n19' href='#n19'>19</a>
<a id='n20' href='#n20'>20</a>
<a id='n21' href='#n21'>21</a>
<a id='n22' href='#n22'>22</a>
<a id='n23' href='#n23'>23</a>
<a id='n24' href='#n24'>24</a>
<a id='n25' href='#n25'>25</a>
<a id='n26' href='#n26'>26</a>
<a id='n27' href='#n27'>27</a>
<a id='n28' href='#n28'>28</a>
<a id='n29' href='#n29'>29</a>
<a id='n30' href='#n30'>30</a>
<a id='n31' href='#n31'>31</a>
<a id='n32' href='#n32'>32</a>
<a id='n33' href='#n33'>33</a>
<a id='n34' href='#n34'>34</a>
<a id='n35' href='#n35'>35</a>
</pre></td>
<td class='lines'><pre><code>