// #region Imports
/**
 * Provides a centralized registry for tour anchors.
 *
 * @internal
 */
class TourRegistry {
    // #region Fields
    _anchors;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `TourRegistry` class.
     *
     * @public
     */
    constructor() {
        this._anchors = new Map();
    }
    // #endregion
    // #region Methods
    /**
     * Registers an anchor element for a tour.
     *
     * @public
     */
    register(tourId, anchorId, element) {
        if (!tourId || !anchorId) {
            return;
        }
        this._anchors.set(this.getKey(tourId, anchorId), element);
    }
    /**
     * Removes a previously registered anchor element.
     *
     * @public
     */
    unregister(tourId, anchorId) {
        if (!tourId || !anchorId) {
            return;
        }
        this._anchors.delete(this.getKey(tourId, anchorId));
    }
    /**
     * Resolves a registered anchor element.
     *
     * @public
     */
    resolve(tourId, anchorId) {
        if (!tourId || !anchorId) {
            return null;
        }
        return this._anchors.get(this.getKey(tourId, anchorId)) ?? null;
    }
    /**
     * Clears all registered anchors.
     *
     * @public
     */
    clear() {
        this._anchors.clear();
    }
    /**
     * Creates a registry key for a tour and anchor.
     *
     * @private
     */
    getKey(tourId, anchorId) {
        return `${tourId}::${anchorId}`;
    }
}
/**
 * The global tour registry instance.
 *
 * @internal
 */
const TOUR_REGISTRY = new TourRegistry();
/**
 * A reactive controller that manages anchor registration for tour elements.
 *
 * @public
 */
export class TourRegistryController {
    // #region Fields
    _host;
    _config;
    _isAnchor;
    _registeredTourId;
    _registeredAnchorId;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `TourRegistryController` class.
     *
     * @public
     */
    constructor(host, config) {
        this._host = host;
        this._config = config;
        this._isAnchor = 'getAnchorId' in config;
        this._registeredTourId = null;
        this._registeredAnchorId = null;
        host.addController(this);
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    hostConnected() {
        if (this._isAnchor) {
            this.register();
        }
    }
    /**
     * @public
     */
    hostDisconnected() {
        if (this._isAnchor) {
            this.unregister();
        }
    }
    /**
     * @public
     */
    hostUpdate() { }
    /**
     * @public
     */
    hostUpdated() { }
    /**
     * Registers the anchor with current tourId and anchorId.
     * Call this when tourId or anchorId changes.
     *
     * @public
     */
    register() {
        if (!this._isAnchor) {
            return;
        }
        const config = this._config;
        const tourId = config.getTourId();
        const anchorId = config.getAnchorId();
        if (!tourId || !anchorId) {
            return;
        }
        TOUR_REGISTRY.register(tourId, anchorId, this._host);
        this._registeredTourId = tourId;
        this._registeredAnchorId = anchorId;
    }
    /**
     * Unregisters the anchor with the previously registered tourId and anchorId.
     *
     * @public
     */
    unregister() {
        if (!this._isAnchor) {
            return;
        }
        if (this._registeredTourId && this._registeredAnchorId) {
            TOUR_REGISTRY.unregister(this._registeredTourId, this._registeredAnchorId);
            this._registeredTourId = null;
            this._registeredAnchorId = null;
        }
    }
    /**
     * Updates the registration when tourId or anchorId changes.
     *
     * @public
     */
    updateRegistration(previousTourId, previousAnchorId) {
        if (!this._isAnchor) {
            return;
        }
        if (this._registeredTourId === previousTourId && this._registeredAnchorId === previousAnchorId) {
            this.unregister();
        }
        this.register();
    }
    /**
     * Resolves an anchor element by its anchorId.
     *
     * @public
     */
    resolve(anchorId) {
        const tourId = this._config.getTourId();
        return TOUR_REGISTRY.resolve(tourId, anchorId);
    }
    /**
     * Clears all registered anchors.
     *
     * @public
     */
    clear() {
        TOUR_REGISTRY.clear();
    }
}
/**
 * Adds a tour registry controller to the given host for anchor registration.
 *
 * @public
 * @param host The host element.
 * @param config The configuration options.
 * @returns The tour registry controller instance.
 */
export function addTourRegistryController(host, config) {
    return new TourRegistryController(host, config);
}
//# sourceMappingURL=TourRegistryController.js.map