/**
 * 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 "../../../types/base.mjs";
import { equipWithInternal } from "../../../types/internal.mjs";

export { Settings };

/**
 * This class is used to manage the settings of the filter.
 *
 * @copyright Volker Schukai
 * @summary A class to manage the settings
 */
class Settings extends Base {
	constructor() {
		super();

		/**
		 * - attachInternalObserver
		 * - detachInternalObserver
		 * - containsInternalObserver
		 * - setInternal
		 * - setInternals
		 * - getInternal
		 */
		equipWithInternal.call(this);
	}

	/**
	 *
	 * @param value
	 * @return {*}
	 */
	get(value) {
		return this.getInternal("option." + value);
	}

	/**
	 * value = "id"
	 *
	 * @param value
	 * @param label
	 * @param visible
	 * @return {Settings}
	 */
	set({ value, label, visible = true }) {
		let d = this.getInternal("option." + value);
		if (d) {
			if (d.label !== label && label) {
				d.label = label;
			}

			if (d.visible !== visible && visible !== undefined) {
				d.visible = visible;
			}
		} else {
			d = {
				value,
				label,
				visible,
			};
		}

		this.setInternal("option." + value, d);
		return this;
	}

	/**
	 *
	 * @return {Array}
	 */
	getOptions() {
		const option = this.getInternal("option");

		const options = [];
		for (const key in option) {
			options.push({
				label: option[key].label,
				value: option[key].value,
				visible: option[key].visible,
			});
		}

		return options;
	}

	/**
	 *
	 * @param options
	 * @return {Settings}
	 */
	setOptions(options) {
		for (const key in options) {
			this.set(options[key]);
		}
		return this;
	}

	/**
	 *
	 * @return {Array}
	 */
	getSelected() {
		const option = this.getInternal("option");

		const selectedValues = [];
		for (const key in option) {
			if (option[key].visible) {
				selectedValues.push(option[key].value);
			}
		}

		return selectedValues;
	}
}
