// #region Imports
import { render } from 'lit';
// #endregion
/**
 * The `Template` class.
 *
 * @public
 */
export class Template {
    // #region Ctor
    /**
     * Constructs a new instance of the `Template` class.
     *
     * @public
     */
    constructor() {
    }
    // #endregion
    // #region Methods
    /**
     * Applies the template to the container.
     *
     * @public
     * @param container - The container element.
     * @param template - The optional HTML template element.
     */
    apply(container, template) {
        if (!template) {
            return;
        }
        if (!(template instanceof HTMLTemplateElement)) {
            throw new Error('Invalid template type!');
        }
        const clone = template.content.cloneNode(true);
        container.appendChild(clone);
    }
    /**
     * Creates a new template.
     *
     * @public
     * @param template - The template string.
     * @returns The template.
     *
     * @example
     * ```ts
     * const template = Template.create(html`<template id="myTemplate"><div>Test</div></template>`);
     * ```
     */
    create(template) {
        // Render the template into a temporary container
        const tempContainer = document.createElement('div');
        render(template, tempContainer);
        // Extract the first child from the container
        const firstChild = tempContainer.firstElementChild;
        // Check if the first child is a <template> element
        if (!(firstChild instanceof HTMLTemplateElement)) {
            throw new Error('The template must start with a <template> tag');
        }
        // Return the template element
        return firstChild;
    }
}
/**
 * @public
 */
export class TemplateServiceLocator {
    // #region Fields
    static _current = null;
    // #endregion
    // #region Properties
    static get current() {
        if (!TemplateServiceLocator.isSet()) {
            throw new Error('\'Template\' not set.');
        }
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return TemplateServiceLocator._current;
    }
    // #endregion
    // #region Methods
    static isSet() {
        return TemplateServiceLocator._current !== null;
    }
    static set(current) {
        TemplateServiceLocator._current = current;
    }
}
TemplateServiceLocator.set(new Template());
//# sourceMappingURL=Template.js.map