<commit_before>import { Action } from '../lib/Action';
import { Util } from '@aegis-framework/artemis';

export class Choice extends Action {

	static setup () {
		this.engine.global ('_CurrentChoice', []);
		this.engine.global ('_ChoiceTimer', []);

		this.engine.global ('_choice_pending_rollback', []);
		this.engine.global ('_choice_just_rolled_back', []);

		this.engine.history ('choice');

		return Promise.resolve ();
	}


	static afterRevert () {
		// When a choice gets reverted, it pushes a `true` value to this global variable.
		// As soon as it gets reverted, this function is run and it pops the `true` out of
		// the array, meaning it was just reverted and the choice should be showing on screeen again.
		if (this.engine.global ('_choice_just_rolled_back').pop ()) {
			return Promise.resolve ();
		}

		// If the player reverts once more while the choice is being shown, then we'll reach this part
		// and we can clean up any variables we need to.
		if (this.engine.global ('_choice_pending_rollback').pop ()) {
			this.engine.global ('_ChoiceTimer').pop ();
		}
		return Promise.resolve ();
	}

	static bind () {
		const engine = this.engine;
		// Bind the click event on data-do elements. This property is used for
		// every choice button.
		this.engine.on ('click', '[data-choice]:not([disabled])', function (event) {
			engine.debug.debug ('Registered Click on Choice Button');
			event.stopImmediatePropagation ();
			event.stopPropagation ();
			event.preventDefault ();

			engine.global ('block', false);

			let doAction = this.dataset.do;

			// Check that the data property was not created with
			// a null property
			if (doAction != 'null') {

				// Remove all the choices
				engine.element ().find ('choice-container').remove ();

				const choice = this.dataset.choice;

				const current = engine.global ('_CurrentChoice').pop().Choice;

				if (typeof current.Timer !== 'undefined') {
					const timer = engine.global ('_ChoiceTimer').pop ();
					engine.global ('_choice_pending_rollback').pop ();
					if (typeof timer !== 'undefined') {
						clearTimeout (timer.props.timer);
						if (timer.parentNode !== null) {
							timer.element ().remove ();
						}
					}
				}

				if (current) {
					doAction = current[choice].Do;
				}

				const run = () => {
					engine.global ('_executing_sub_action', true);
					engine.run (doAction).then ((result) => {
						engine.global ('_executing_sub_action', false);
						engine.history ('choice').push (choice);

						return Promise.resolve (result);
					});
				};

				// Remove the reference to the current choice object
				if (current) {
					if (current[choice] !== 'undefined') {
						if (typeof current[choice].onChosen === 'function') {
							return Util.callAsync (current[choice].onChosen, engine).then (() => {
								run ();
							});
						}
					}
				}
				run ();
			}
		});
		return Promise.resolve ();
	}

	static reset () {
		this.engine.global ('_CurrentChoice', []);
		this.engine.global ('_ChoiceTimer', []);
		return Promise.resolve ();
	}

	static matchObject (statement) {
		return typeof statement.Choice !== 'undefined';
	}

	constructor (statement) {
		super ();

		this.statement = statement.Choice;

		this.result = { advance: false, step: false };
	}

	apply () {
		this.engine.global ('block', true);

		// Save a reference to the choice object globally. Since the choice buttons
		// are set a data-do property to know what the choice should do, it is
		// limited to a string and thus object or function actions would not be
		// able to be used in choices.
		this.engine.global ('_CurrentChoice').push (this._statement);

		const promises = [];
<commit_msg>Clear all globals after reset<commit_after>import { Action } from '../lib/Action';
import { Util } from '@aegis-framework/artemis';

export class Choice extends Action {

	static setup () {
		this.engine.global ('_CurrentChoice', []);
		this.engine.global ('_ChoiceTimer', []);

		this.engine.global ('_choice_pending_rollback', []);
		this.engine.global ('_choice_just_rolled_back', []);

		this.engine.history ('choice');

		return Promise.resolve ();
	}


	static afterRevert () {
		// When a choice gets reverted, it pushes a `true` value to this global variable.
		// As soon as it gets reverted, this function is run and it pops the `true` out of
		// the array, meaning it was just reverted and the choice should be showing on screeen again.
		if (this.engine.global ('_choice_just_rolled_back').pop ()) {
			return Promise.resolve ();
		}

		// If the player reverts once more while the choice is being shown, then we'll reach this part
		// and we can clean up any variables we need to.
		if (this.engine.global ('_choice_pending_rollback').pop ()) {
			this.engine.global ('_ChoiceTimer').pop ();
		}
		return Promise.resolve ();
	}

	static bind () {
		const engine = this.engine;
		// Bind the click event on data-do elements. This property is used for
		// every choice button.
		this.engine.on ('click', '[data-choice]:not([disabled])', function (event) {
			engine.debug.debug ('Registered Click on Choice Button');
			event.stopImmediatePropagation ();
			event.stopPropagation ();
			event.preventDefault ();

			engine.global ('block', false);

			let doAction = this.dataset.do;

			// Check that the data property was not created with
			// a null property
			if (doAction != 'null') {

				// Remove all the choices
				engine.element ().find ('choice-container').remove ();

				const choice = this.dataset.choice;

				const current = engine.global ('_CurrentChoice').pop().Choice;

				if (typeof current.Timer !== 'undefined') {
					const timer = engine.global ('_ChoiceTimer').pop ();
					engine.global ('_choice_pending_rollback').pop ();
					if (typeof timer !== 'undefined') {
						clearTimeout (timer.props.timer);
						if (timer.parentNode !== null) {
							timer.element ().remove ();
						}
					}
				}

				if (current) {
					doAction = current[choice].Do;
				}

				const run = () => {
					engine.global ('_executing_sub_action', true);
					engine.run (doAction).then ((result) => {
						engine.global ('_executing_sub_action', false);
						engine.history ('choice').push (choice);

						return Promise.resolve (result);
					});
				};

				// Remove the reference to the current choice object
				if (current) {
					if (current[choice] !== 'undefined') {
						if (typeof current[choice].onChosen === 'function') {
							return Util.callAsync (current[choice].onChosen, engine).then (() => {
								run ();
							});
						}
					}
				}
				run ();
			}
		});
		return Promise.resolve ();
	}

	static reset () {
		this.engine.global ('_CurrentChoice', []);
		this.engine.global ('_ChoiceTimer', []);

		this.engine.global ('_choice_pending_rollback', []);
		this.engine.global ('_choice_just_rolled_back', []);
		return Promise.resolve ();
	}

	static matchObject (statement) {
		return typeof statement.Choice !== 'undefined';
	}

	constructor (statement) {
		super ();

		this.statement = statement.Choice;

		this.result = { advance: false, step: false };
	}

	apply () {
		this.engine.global ('block', true);

		// Save a reference to the choice object globally. Since the choice buttons
		// are set a data-do property to know what the choice should do, it is
		// limited to a string and thus object or function actions would not be
		// able to be used in choices.
		this.engine.global ('_CurrentChoice').push (this._statement);

		const promises = [];

{'fraction_non_alphanumeric': 0.14173438124417212, 'fraction_numerical': 0.0, 'mean_word_length': 3.111719605695509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6399169', 'n_tokens_mistral': 2628, 'n_tokens_neox': 2150, 'n_words': 668}