/**
 * 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 { internalSymbol } from "../../constants.mjs";
import { Base } from "../../types/base.mjs";
import { getGlobal, getGlobalFunction } from "../../types/global.mjs";
import { isFunction } from "../../types/is.mjs";
import { validateInstance, validateString } from "../../types/validate.mjs";

export { Factory };

/**
 * A factory for creating worker instances.
 *
 * @license AGPLv3
 * @since 1.25.0
 * @copyright Volker Schukai
 * @summary A small factory to create worker
 */
class Factory extends Base {
	/**
	 *
	 */
	constructor() {
		super();
		this[internalSymbol] = {
			worker: new WeakMap(),
		};
	}

	/**
	 * Creates a worker from a URL
	 *
	 * @param {string|URL} url
	 * @param {function} messageHandler
	 * @param {function} errorHandler
	 * @return {Worker}
	 */
	createFromURL = function (url, messageHandler, errorHandler) {
		if (url instanceof URL) {
			url = url.toString();
		}

		const workerClass = getGlobalFunction("Worker");
		var worker = new workerClass(validateString(url));

		if (isFunction(messageHandler)) {
			worker.onmessage = (event) => {
				messageHandler.call(worker, event);
			};
		}

		if (isFunction(errorHandler)) {
			worker.onerror = (event) => {
				errorHandler.call(worker, event);
			};
		}

		return worker;
	};

	/**
	 * Creates a worker from a script
	 *
	 * @param {string} content
	 * @param {function} messageHandler
	 * @param {function} errorHandler
	 * @return {Worker}
	 * @see https://developer.mozilla.org/de/docs/Web/API/URL/createObjectURL
	 */
	createFromScript = function (content, messageHandler, errorHandler) {
		const blobFunction = new getGlobalFunction("Blob");
		const blob = new blobFunction([validateString(content)], {
			type: "script/javascript",
		});

		const url = getGlobalFunction("URL").createObjectURL(blob);
		const worker = this.createFromURL(url, messageHandler, errorHandler);

		this[internalSymbol]["worker"].set(worker, url);

		return worker;
	};

	/**
	 * Terminate the worker and call revokeObjectURL if necessary.
	 *
	 * @param worker
	 * @return {Monster.DOM.Worker.Factory}
	 */
	terminate(worker) {
		const workerClass = getGlobalFunction("Worker");
		validateInstance(worker, workerClass);

		worker.terminate();

		if (this[internalSymbol]["worker"].has(worker)) {
			const url = this[internalSymbol]["worker"].get(worker);
			URL.revokeObjectURL(url);
		}

		return this;
	}
}
