import { UmbControllerBase } from '../../../../libs/class-api/index.js';
import { UmbArrayState, UmbStringState } from '../../../../libs/observable-api/index.js';
/**
 * The workspace route manager.
 * @class UmbWorkspaceRouteManager
 * @augments {UmbControllerBase}
 */
export class UmbWorkspaceRouteManager extends UmbControllerBase {
    constructor() {
        super(...arguments);
        //
        this.#routes = new UmbArrayState([], (x) => x.path);
        this.routes = this.#routes.asObservable();
        this.#activeLocalPath = new UmbStringState('');
    }
    //
    #routes;
    #activeLocalPath;
    /**
     * Set the routes for the workspace.
     * @param {Array<UmbRoute>} routes The routes for the workspace.
     * @memberof UmbWorkspaceRouteManager
     */
    setRoutes(routes) {
        const allRoutes = [...routes];
        if (routes.length > 0) {
            allRoutes.push({
                path: `**`,
                component: async () => (await import('../../router/index.js')).UmbRouteNotFoundElement,
            });
        }
        const mappedRoutes = allRoutes.map((route) => {
            // override the setup method to set the active local path
            const oldSetupCallback = route.setup;
            route.setup = (_component, info) => {
                this.#activeLocalPath.setValue(info.match.fragments.consumed);
                if (oldSetupCallback) {
                    oldSetupCallback(_component, info);
                }
            };
            return route;
        });
        this.#routes.setValue([...mappedRoutes]);
    }
    /**
     * Get the routes for the workspace.
     * @returns {Array<UmbRoute>} The routes for the workspace.
     * @memberof UmbWorkspaceRouteManager
     */
    getRoutes() {
        return this.#routes.getValue();
    }
    /**
     * Get the active local path.
     * @returns {*}  {string}
     * @memberof UmbWorkspaceRouteManager
     */
    getActiveLocalPath() {
        return this.#activeLocalPath.getValue();
    }
}
