/**
 * FeedbackWidget — Floating feedback button + panel.
 * Include on any demo page. Uses window.DEMO_API for endpoint.
 * Usage: React.createElement(FeedbackWidget, { gameName: 'Base Builder' })
 */
var FeedbackWidget = (function() {
    'use strict';
    var h = React.createElement;
    var useState = React.useState;

    function FeedbackWidget(props) {
        var gameName = props.gameName || '';
        var open = useState(false);
        var isOpen = open[0], setOpen = open[1];
        var type = useState('general');
        var fbType = type[0], setType = type[1];
        var rating = useState(0);
        var fbRating = rating[0], setRating = rating[1];
        var comment = useState('');
        var fbComment = comment[0], setComment = comment[1];
        var status = useState('idle'); // idle | sending | success | error
        var fbStatus = status[0], setStatus = status[1];

        function handleSubmit() {
            if (!fbComment.trim()) return;
            setStatus('sending');
            fetch(window.DEMO_API + '?action=feedback', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    gameName: gameName,
                    type: fbType,
                    rating: fbRating || null,
                    comment: fbComment,
                    pageURL: window.location.href
                })
            })
            .then(function(r) { return r.json(); })
            .then(function() {
                setStatus('success');
                setTimeout(function() {
                    setStatus('idle');
                    setOpen(false);
                    setComment('');
                    setRating(0);
                    setType('general');
                }, 2000);
            })
            .catch(function() { setStatus('error'); });
        }

        // Floating button
        var btn = h('button', {
            className: 'dp-fb-btn',
            onClick: function() { setOpen(!isOpen); },
            title: 'Send Feedback'
        }, '\u2709');

        if (!isOpen) return btn;

        // Star rating
        var stars = h('div', { className: 'dp-fb-stars' },
            [1, 2, 3, 4, 5].map(function(n) {
                return h('button', {
                    key: n,
                    className: 'dp-fb-star' + (n <= fbRating ? ' active' : ''),
                    onClick: function() { setRating(n); }
                }, '\u2605');
            })
        );

        // Success state
        if (fbStatus === 'success') {
            return h('div', null,
                btn,
                h('div', { className: 'dp-fb-panel' },
                    h('div', { className: 'dp-fb-success' }, 'Thank you for your feedback!')
                )
            );
        }

        // Form
        var panel = h('div', { className: 'dp-fb-panel' },
            h('div', { className: 'dp-fb-header' },
                h('span', null, 'Send Feedback'),
                h('button', { className: 'dp-fb-close', onClick: function() { setOpen(false); } }, '\u2715')
            ),
            h('div', { className: 'dp-fb-body' },
                h('label', null, 'Type'),
                h('select', { value: fbType, onChange: function(e) { setType(e.target.value); } },
                    h('option', { value: 'general' }, 'General Comment'),
                    h('option', { value: 'bug' }, 'Bug Report'),
                    h('option', { value: 'suggestion' }, 'Suggestion'),
                    h('option', { value: 'praise' }, 'Something I Liked')
                ),
                h('label', null, 'Rating (optional)'),
                stars,
                h('label', null, 'Your feedback'),
                h('textarea', {
                    value: fbComment,
                    onChange: function(e) { setComment(e.target.value); },
                    placeholder: 'Tell us what you think...'
                }),
                h('button', {
                    className: 'dp-fb-submit',
                    disabled: !fbComment.trim() || fbStatus === 'sending',
                    onClick: handleSubmit
                }, fbStatus === 'sending' ? 'Sending...' : 'Submit Feedback')
            )
        );

        return h('div', null, btn, panel);
    }

    return FeedbackWidget;
})();
