// #region Imports
import { ControllerBase } from './ControllerBase';
/**
 * The abstract `ElementController` class.
 *
 * @public
 * @abstract
 */
export class ElementController extends ControllerBase {
    // #region Fields
    _config;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `ElementController` class.
     *
     * @public
     */
    constructor(host, config) {
        super(host);
        this._config = config;
    }
    // #endregion
    // #region Methods
    /**
     * Abstract method to get a typed element with `TWrapper` functionality.
     *
     * @public
     * @abstract
     * @param name - The name of the element.
     * @returns An element of type `TWrapper`, or `null` if not und.
     */
    get(name) {
        const query = Object.entries(this._config.queries ?? {}).find(([key]) => key === name);
        const element = this.tryQueryElement(this.host, query?.[1] ?? '');
        if (!element) {
            return null;
        }
        if (!('name' in element)) {
            throw new Error('Element does not have a name property.');
        }
        return this.map(element);
    }
    /**
     * Abstract method to get a typed element with `TWrapper` functionality asynchronously.
     *
     * @public
     * @param name - The name of the element.
     * @returns A promise that resolves with an element of type `TWrapper`, or `null` if not und.
     */
    async getAsync(name) {
        const query = Object.entries(this._config.queries ?? {}).find(([key]) => key === name);
        const element = await this.tryQueryElementAsync(this.host, query?.[1] ?? '');
        if (!element) {
            return null;
        }
        if (!('name' in element)) {
            throw new Error('Element does not have a name property.');
        }
        return this.map(element);
    }
    /**
     * @private
     */
    tryQueryElement(root, selector) {
        const result = root.renderRoot.querySelector(selector) ?? null;
        if (result === null && !root.hasUpdated) {
            console.warn(`Element with selector '${selector}' not und. This may be due to the element not being rendered yet.`);
        }
        return result;
    }
    /**
     * @private
     */
    tryQueryElementAsync(root, selector) {
        return root.updateComplete.then(() => this.tryQueryElement(root, selector));
    }
}
//# sourceMappingURL=ElementController.js.map