/**
 * 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 { extend } from "../../data/extend.mjs";
import {
	ATTRIBUTE_CLASS,
	ATTRIBUTE_HREF,
	ATTRIBUTE_ID,
	ATTRIBUTE_NONCE,
	ATTRIBUTE_SRC,
	ATTRIBUTE_TITLE,
	ATTRIBUTE_TYPE,
	TAG_LINK,
} from "../constants.mjs";
import { KEY_DOCUMENT, referenceSymbol, Resource } from "../resource.mjs";
import { instanceSymbol } from "../../constants.mjs";
export { Link };

/**
 * This class is used by the resource manager to embed external resources.
 *
 * @fragments /fragments/libraries/dom/resource/link/
 *
 * @example /examples/libraries/dom/resource/link/simple/ Create and connect a link resource
 * @license AGPLv3
 * @since 1.25.0
 * @copyright Volker Schukai
 * @summary A link resource for attaching external link-based assets such as stylesheets or preloads.
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
 */
class Link extends Resource {
	/**
	 * @property {string} as {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-as}
	 * @property {string} crossOrigin=anonymous {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin}
	 * @property {boolean} disabled
	 * @property {string} href {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-href}
	 * @property {string} hreflang {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-hreflang}
	 * @property {string} imagesizes {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-imagesizes}
	 * @property {string} imagesrcset {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-imagesrcset}
	 * @property {string} integrity {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-integrity}
	 * @property {string} media {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-media}
	 * @property {string} prefetch {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-prefetch}
	 * @property {string} referrerpolicy {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-referrerpolicy}
	 * @property {string} rel {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-rel}
	 * @property {string} type {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-type}
	 * @property {string} sizes {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-sizes}
	 * @property {string} nonce {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nonce}
	 */
	get defaults() {
		return extend({}, super.defaults, {
			as: undefined,
			crossOrigin: "anonymous",
			disabled: undefined,
			href: undefined,
			hreflang: undefined,
			imagesizes: undefined,
			imagesrcset: undefined,
			integrity: undefined,
			media: undefined,
			prefetch: undefined,
			referrerpolicy: undefined,
			rel: undefined,
			sizes: undefined,
			type: undefined,
			nonce: undefined,
		});
	}

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

	/**
	 *
	 * @return {Monster.DOM.Resource.Link}
	 */
	create() {
		createElement.call(this);
		return this;
	}

	/**
	 * @return {string}
	 */
	static getURLAttribute() {
		return ATTRIBUTE_HREF;
	}
}

/**
 * @private
 * @return {Monster.DOM.Resource.Link}
 */
function createElement() {
	const document = this.getOption(KEY_DOCUMENT);
	this[referenceSymbol] = document.createElement(TAG_LINK);

	for (const key of [
		"as",
		"crossOrigin",
		"disabled",
		"href",
		"hreflang",
		"imagesizes",
		"imagesrcset",
		"integrity",
		"media",
		"prefetch",
		"referrerpolicy",
		"sizes",
		"rel",
		"type",
		ATTRIBUTE_HREF,
		ATTRIBUTE_ID,
		ATTRIBUTE_CLASS,
		ATTRIBUTE_TITLE,
		ATTRIBUTE_NONCE,
	]) {
		if (this.getOption(key) !== undefined) {
			this[referenceSymbol][key] = this.getOption(key);
		}
	}

	return this;
}
