/**
 * 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 {
	assembleMethodSymbol,
	registerCustomElement,
} from "../../../dom/customelement.mjs";
import { CustomControl } from "../../../dom/customcontrol.mjs";
import { FilterControlsDefaultsStyleSheet } from "../stylesheet/filter-controls-defaults.mjs";
import { FilterStyleSheet } from "../stylesheet/filter.mjs";

export { DateTimeFilter };

/**
 * local symbol
 * @private
 * @type {symbol}
 */
const inputElementSymbol = Symbol("inputElement");

/**
 * The FilterDateTime component is used to filter by a date and time value.
 *
 * @fragments /fragments/components/datatable/filter/date-time
 *
 * @example /examples/components/datatable/filter-controls Filter controls
 *
 * @copyright Volker Schukai
 * @summary The FilterDateTime component is used to show and handle datetime values.
 */
class DateTimeFilter extends CustomControl {
	/**
	 * This method is called by the `instanceof` operator.
	 * @return {symbol}
	 */
	static get [instanceSymbol]() {
		return Symbol.for("@schukai/monster/components/filter/date-time@@instance");
	}

	/**
	 * To set the options via the HTML tag, the attribute `data-monster-options` must be used.
	 * @see {@link https://monsterjs.org/en/doc/#configurate-a-monster-control}
	 *
	 * The individual configuration values can be found in the table.
	 *
	 * @property {Object} templates Template definitions
	 * @property {string} templates.main Main template
	 * @property {object} datasource The datasource
	 * @property {boolean} autoLoad If true, the datasource is called immediately after the control is created.
	 */
	get defaults() {
		return Object.assign({}, super.defaults, {
			templates: {
				main: getTemplate(),
			},
		});
	}

	/**
	 *
	 * @return {string}
	 */
	static getTag() {
		return "monster-filter-date-time";
	}

	/**
	 *
	 * @return {FilterButton}
	 */
	[assembleMethodSymbol]() {
		super[assembleMethodSymbol]();

		initControlReferences.call(this);
		initEventHandler.call(this);
	}

	/**
	 * @return Array<CSSStyleSheet>
	 */
	static getCSSStyleSheet() {
		return [FilterControlsDefaultsStyleSheet, FilterStyleSheet];
	}

	/**
	 * This is a method of [internal api](https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals)
	 *
	 * @param {*} value
	 */
	set value(value) {
		this[inputElementSymbol].value = value;
	}

	/**
	 * @return {*}
	 */
	get value() {
		return this[inputElementSymbol].value;
	}
}

/**
 * @private
 * @return {DateTimeFilter}
 */
function initControlReferences() {
	if (!this.shadowRoot) {
		throw new Error("no shadow-root is defined");
	}

	this[inputElementSymbol] = this.shadowRoot.querySelector(
		"[data-monster-role=query]",
	);
	return this;
}

/**
 * @private
 */
function initEventHandler() {}

/**
 * @private
 * @return {string}
 */
function getTemplate() {
	// language=HTML
	return `
        <div data-monster-role="control" part="control">
            <slot></slot>
            <input type="datetime-local" name="query" data-monster-role="query" data-monster-attributes="value path:query">
        </div>
    `;
}

registerCustomElement(DateTimeFilter);
