export default class PopupStates {
	/**
	 *
	 * @param {PopupState} state
	 * @return {number}
	 */
	push(state) {
		const states = this._states()
		states.id++

		state.id = states.id
		states.list.push(state)
		return states.id
	}

	/**
	 *
	 * @returns {PopupState|null}
	 */
	pop() {
		const states = this._states()
		if (states.list.length > 0) {
			return states.list.pop()
		}
		return null
	}

	/**
	 * @param {Number} id
	 * @returns {PopupState|null}
	 */
	get(id) {
		const states = this._states()
		for (const state of states.list) {
			if (state.id == id) {
				return state
			}
		}
		return null
	}

	/**
	 *
	 * @returns {PopupState}|null}
	 */
	last() {
		const states = this._states()
		if (states.list.length > 0) {
			return states.list[states.list.length - 1]
		}
		return null
	}

	/**
	 *
	 * @returns {Number}
	 */
	get length() {
		const states = this._states()
		return states.list.length
	}

	clear() {
		const states = this._states()
		states.list = []
	}

	/**
	 *
	 * @returns {{id: Number, list: Array.<PopupState>}}
	 * @private
	 */
	_states() {
		if (window.X_POPUP_STATES == null || typeof window.X_POPUP_STATES !== "object") {
			window.X_POPUP_STATES = {
				id: 0,
				list: []
			}
		}
		return window.X_POPUP_STATES
	}
}

class PopupState {
	/**
	 * @type {Number}
	 */
	id

	/**
	 * @type {Object}
	 */
	data

	/**
	 * @type {{parent:String, path:String}}
	 */
	action

	/**
	 * @type {Object}
	 */
	ctx

	/**
	 * @type {Function}
	 */
	callback

	/**
	 * @type {{close:Function}}
	 */
	dialogInstance

	/**
	 * @type {String}
	 */
	url
}
