import { Component, Fragment } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';
import Buttons from './buttons';
import { MaybeShowButton } from '../buttons';
import { default as ButtonCheck } from '../buttons/button-check';
import { default as ButtonHint } from '../buttons/button-hint';
const $ = window.jQuery;
const { uniqueId, isArray, isNumber, bind } = lodash;
class Question extends Component {
constructor() {
super( ...arguments );
this.state = {
time: null,
showHint: false,
};
this.$wrap = null;
}
componentDidMount( a ) {
const { question, isCurrent, markQuestionRendered } = this.props;
if ( isCurrent ) {
markQuestionRendered( question.id );
}
if ( ! this.state.time ) {
this.setState( {
time: new Date(),
} );
}
LP.Hook.doAction( 'lp-question-compatible-builder' );
if (
typeof MathJax !== 'undefined' &&
typeof MathJax.Hub !== 'undefined'
) {
MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] );
}
return a;
}
setRef = ( el ) => {
this.$wrap = $( el );
};
parseOptions = ( options ) => {
if ( options ) {
options = ! isArray( options )
? JSON.parse(
CryptoJS.AES.decrypt( options.data, options.key, {
format: CryptoJSAesJson,
} ).toString( CryptoJS.enc.Utf8 )
)
: options;
options = ! isArray( options ) ? JSON.parse( options ) : options;
}
return options || [];
};
getWrapperClass = () => {
const { question, answered } = this.props;
const classes = [ 'question', 'question-' + question.type ];
const options = this.parseOptions( question.options );
if ( options.length && options[ 0 ].isTrue !== undefined ) {
classes.push( 'question-answered' );
}
return classes;
};
getEditLink = () => {
const { question, editPermalink } = this.props;
return editPermalink
? editPermalink.replace( /post=(.*[0-9])/, `post=${ question.id }` )
: '';
};
editPermalink = ( editPermalink ) => {
return sprintf(
'%s',
editPermalink,
__( 'Edit', 'learnpress' )
);
};
render() {
const { question, isShow, isShowIndex, isShowHint, status, results } =
this.props;
if (
results &&
results.questions &&
results.questions[ question.id ]
) {
if (
typeof results.questions[ question.id ].correct !== 'undefined'
) {
question.correct = results.questions[ question.id ].correct;
}
}
const QuestionTypes = LP.questionTypes.default;
const editPermalink = this.getEditLink();
if ( editPermalink ) {
jQuery( '#wp-admin-bar-edit-lp_question' )
.find( '.ab-item' )
.attr( 'href', editPermalink );
}
const titleParts = {
index: () => {
return isShowIndex ? (
{ isShowIndex }.
) : (
''
);
},
title: () => {
return (
);
},
hint: () => {
return