/**
 * 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 { instanceSymbol } from "../../constants.mjs";
import { findElementWithSelectorUpwards } from "../../dom/util.mjs";
import { isObject } from "../../types/is.mjs";
import { Datasource } from "../datasource.mjs";

export { DomStorage };

/**
 * The DomStorage is a class that stores data in memory.
 *
 * @license AGPLv3
 * @copyright Volker Schukai
 */
class DomStorage extends Datasource {
	/**
	 * @param {Object} [options] options contains definitions for the datasource.
	 */
	constructor(options) {
		super();

		if (isObject(options)) {
			this.setOptions(options);
		}
	}

	/**
	 * This method is called by the `instanceof` operator.
	 * @return {symbol}
	 */
	static get [instanceSymbol]() {
		return Symbol.for("@schukai/monster/data/datasource/storage/dom-storage");
	}

	/**
	 * @property {Object} defaults
	 * @property {Object} defaults.read
	 * @property {string} defaults.read.selector
	 * @property {Object} defaults.write
	 * @property {string} defaults.write.selector
	 */
	get defaults() {
		return Object.assign({}, super.defaults, {
			read: {
				selector: undefined,
			},
			write: {
				selector: undefined,
			},
		});
	}

	/**
	 * @return {Promise}
	 * @throws {Error} The read selector is not defined
	 * @throws {Error} There are no storage element
	 */
	read() {
		const selector = this.getOption("read.selector", undefined);
		if (!selector) {
			throw new Error("The read selector is not defined");
		}

		const storage = findElementWithSelectorUpwards(this, selector);
		if (!storage) {
			throw new Error("There is no storage element");
		}

		return new Promise((resolve, reject) => {
			try {
				const data = JSON.parse(storage.innerHTML);
				this.set(data);
				resolve(data);
			} catch (e) {
				reject(e);
			}
		});
	}

	/**
	 * @return {Promise}
	 * @throws {Error} The write selector is not defined
	 * @throws {Error} There are no storage element
	 */
	write() {
		const selector = this.getOption("write.selector");
		if (!selector) {
			throw new Error("The option write.selector is not defined");
		}

		const storage = findElementWithSelectorUpwards(this, selector);
		if (!storage) {
			throw new Error("There is no storage element");
		}

		return new Promise((resolve, reject) => {
			try {
				storage.innerHTML = JSON.stringify(this.get());
				resolve(storage);
			} catch (e) {
				reject(e);
			}
		});
	}
}
