import React from 'react';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import { MenuBar } from './menubar';
import { Container, FormBuilder, NgIf, Icon, Fab } from '../../components/';
import { appendShareToUrl } from '../../helpers/';
import './formviewer.scss';
export class FormViewer extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            form: {}
        };
    }
    componentDidMount(){
        this.setState({form: JSON.parse(this.props.content)});
    }
    onChange(){
        this.setState({refresh: Math.random()});
        if(JSON.stringify(this.state.form) === this.props.content){
            this.props.needSavingUpdate(false);
        } else {
            this.props.needSavingUpdate(true);
        }
    }
    save(){
        if(this.props.needSaving === false) return;
        let blob = new window.Blob([JSON.stringify(this.state.form)], {type : 'text/plain'});
        return this.props
            .onSave(blob)
            .then(() => this.props.needSavingUpdate(false));
    }
    simpleMarkdown(text){
        const regLink = /\[([^\]]*)\]\(([^\)]+)\)/g
        return text
            .replace(regLink, function(str){
                const label = str.replace(regLink, '$1')
                const link = str.replace(regLink, '$2')
                return "["+label+"]("+appendShareToUrl(link)+")";
            })
            .replace(regLink, '$1')
            .replace(/\n/g, "
")
    }
    beautify(label){
        return label
            .split('_')
            .map((t) => {
                if(t.length === 0) return t
                else if(/[gu]?u?id/.test(t.toLowerCase())) return t.toUpperCase();
                return t[0].toUpperCase() + t.substring(1)
            })
            .join(" ");
    }
    render(){
        return (