/**
 * Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved.
 * Node module: @schukai/monster
 *
 * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
 * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
 *
 * For those who do not wish to adhere to the AGPLv3, a commercial license is available.
 * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
 * For more information about purchasing a commercial license, please contact Volker Schukai.
 *
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Base } from "./base.mjs";
import { instanceSymbol } from "../constants.mjs";
export { Stack };

/**
 * You can call the method via the monster namespace `new Queue()`.
 *
 * @license AGPLv3
 * @since 1.4.0
 * @copyright Volker Schukai
 */
class Stack extends Base {
	/**
	 *
	 */
	constructor() {
		super();
		this.data = [];
	}

	/**
	 * This method is called by the `instanceof` operator.
	 * @return {symbol}
	 * @since 2.1.0
	 */
	static get [instanceSymbol]() {
		return Symbol.for("@schukai/monster/types/stack");
	}

	/**
	 * @return {boolean}
	 */
	isEmpty() {
		return this.data.length === 0;
	}

	/**
	 * looks at the object at the top of this stack without removing it from the stack.
	 *
	 * @return {*}
	 */
	peek() {
		if (this.isEmpty()) {
			return undefined;
		}

		return this.data?.[this.data.length - 1];
	}

	/**
	 * pushes an item onto the top of this stack.
	 *
	 * @param {*} value
	 * @return {Queue}
	 */
	push(value) {
		this.data.push(value);
		return this;
	}

	/**
	 * remove all entries
	 *
	 * @return {Queue}
	 */
	clear() {
		this.data = [];
		return this;
	}

	/**
	 * removes the object at the top of this stack and returns
	 * that object as the value of this function. is the stack empty
	 * the return value is undefined.
	 *
	 * @return {*}
	 */
	pop() {
		if (this.isEmpty()) {
			return undefined;
		}
		return this.data.pop();
	}
}
