/* eslint-disable no-undef */
// Return FAQ index number from question or answer ID.
export const getFaqIndex = (faqItemId, separator = '_') => {
	const faqIdPart = faqItemId.split(separator);
	const faqIndex = faqIdPart[faqIdPart.length - 1];
	//("getFaqIndex: ", faqItemId, faqIndex);
	return faqIndex;
};

export const checkEditorContent = (faqIndex) => {
	//console.log('CHK INDEX:', faqIndex);
	// Generate element ID's.
	const questionId = 'question_edit_' + faqIndex;
	const answerId = 'answer_edit_' + faqIndex;
	let qContent, aContent;

	// console.log(
	// 	'CHECK EDITOR TINYMCE',
	// 	Boolean(tinyMCE.editors[questionId])
	// );
	// console.log(
	// 	'CHECK EDITOR TEXTAREA',
	// 	document.querySelector(`#${questionId}`).textContent
	// );

	// Check if question/answer editor content is empty or just contains whitespace.
	// If TinyMCE not instantiated then use textarea value.
	qContent = Boolean(tinyMCE.editors[questionId])
		? tinyMCE.editors[questionId].getContent()
		: document.querySelector(`#${questionId}`).textContent;
	qContent = qContent.replace(/(<([^>]+)>)/gi, '').replace(/\s/g, '');

	aContent = Boolean(tinyMCE.editors[answerId])
		? tinyMCE.editors[answerId].getContent()
		: document.querySelector(`#${answerId}`).textContent;
	aContent = aContent.replace(/(<([^>]+)>)/gi, '').replace(/\s/g, '');

	// console.log('>>> Q:', qContent.trim(), qContent.trim().length);
	// console.log('>>> A:', aContent.trim(), aContent.trim().length);

	return [!qContent.trim(), !aContent.trim()];
};

export const checkEmptyContent = (qContent, aContent) => {
	// Generate element ID's.
	//const questionId = 'question_edit_' + faqIndex;
	//const answerId = 'answer_edit_' + faqIndex;
	//let qContent, aContent;

	// console.log(
	// 	'CHECK EDITOR TINYMCE',
	// 	Boolean(tinyMCE.editors[questionId])
	// );
	// console.log(
	// 	'CHECK EDITOR TEXTAREA',
	// 	document.querySelector(`#${questionId}`).textContent
	// );

	// Check if question/answer editor content is empty or just contains whitespace.
	// If TinyMCE not instantiated then use textarea value.
	//qContent = Boolean(tinyMCE.editors[questionId]) ? tinyMCE.editors[questionId].getContent() : document.querySelector(`#${questionId}`).textContent;
	qContent = qContent.replace(/(<([^>]+)>)/gi, '').replace(/\s/g, '');

	//aContent = Boolean(tinyMCE.editors[answerId]) ? tinyMCE.editors[answerId].getContent() : document.querySelector(`#${answerId}`).textContent;
	aContent = aContent.replace(/(<([^>]+)>)/gi, '').replace(/\s/g, '');

	const empty = !qContent.trim() || !aContent.trim() ? true : false;

	// console.log('>>> Q:', qContent.trim(), qContent.trim().length);
	// console.log('>>> A:', aContent.trim(), aContent.trim().length);

	return empty;
};
