/**
 * 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 { validateInstance, validateString } from "../types/validate.mjs";
import { ATTRIBUTE_ERRORMESSAGE } from "./constants.mjs";

export { resetErrorAttribute, addErrorAttribute, removeErrorAttribute };

/**
 * This method can be used to reset the error attribute of an element.
 * @license AGPLv3
 * @since 4.37.1
 * @param {HTMLElement} element
 * @return {HTMLElement}
 */
function resetErrorAttribute(element) {
	validateInstance(element, HTMLElement);
	if (element.hasAttribute(ATTRIBUTE_ERRORMESSAGE)) {
		element.removeAttribute(ATTRIBUTE_ERRORMESSAGE);
	}
	return element;
}

/**
 * This method can be used to add an error message to an element.
 *
 * @license AGPLv3
 * @since 3.99.0
 * @copyright Volker Schukai
 * @param {HTMLElement} element
 * @param {string} message
 * @return {HTMLElement}
 */
function addErrorAttribute(element, message) {
	validateInstance(element, HTMLElement);

	if (message instanceof Error) {
		message = message.message;
	}

	if (typeof message !== "string") {
		if (typeof message === "object" && message !== null) {
			if (typeof message.toString === "function") {
				message = message.toString();
			} else {
				message = JSON.stringify(message);
			}
		} else {
			message = String(message);
		}
	}

	validateString(message);

	if (!element.hasAttribute(ATTRIBUTE_ERRORMESSAGE)) {
		element.setAttribute(ATTRIBUTE_ERRORMESSAGE, message);
		return element;
	}

	const current = element.getAttribute(ATTRIBUTE_ERRORMESSAGE);
	const list = current.split("::");

	for (let i = 0; i < list.length; i++) {
		if (list[i] === message) {
			return element;
		}
	}

	list.push(message);

	element.setAttribute(ATTRIBUTE_ERRORMESSAGE, list.join("::"));

	return element;
}

/**
 * This method can be used to remove an error message from an element.
 * @license AGPLv3
 * @since 3.99.0
 * @param element
 * @param message
 * @param {HTMLElement} element
 * @param {string} message
 * @return {HTMLElement}
 */
function removeErrorAttribute(element, message) {
	validateInstance(element, HTMLElement);
	validateString(message);

	if (!element.hasAttribute(ATTRIBUTE_ERRORMESSAGE)) {
		return element;
	}

	const current = element.getAttribute(ATTRIBUTE_ERRORMESSAGE);
	const list = current.split("::");
	const newList = list.filter(function (token) {
		return token !== message;
	});

	element.setAttribute(ATTRIBUTE_ERRORMESSAGE, newList.join("::"));

	return element;
}
