/*
 * Copyright 2022-2025 VMware, Inc. All rights reserved. VMware Confidential
 */

import { Injectable } from "@angular/core";
import { ActivatedRouteSnapshot, CanDeactivate, RouterStateSnapshot } from "@angular/router";
import { ExtensionContainerComponent } from "./extension-container.component";
import { ExtensionContainerService } from "./extension-container.service";

@Injectable({ providedIn: "root" })
export class ExtensionContainerGuard implements CanDeactivate<ExtensionContainerComponent> {
    constructor(private extensionContainerService: ExtensionContainerService) {}

    /**
     * Note that the comment in the below method is null all the time (or at least it was).
     * We weren't able to find the root cause of this and because of that ExtensionContainerService
     * is used to keep a reference to the current instance of ExtensionContainerComponent.
     *
     * Note that this might be a problem if you have multiple instances of ExtensionContainerComponent
     * at the same time.
     * @param component
     * @param currentRoute
     * @param currentState
     * @param nextState
     * @returns
     */
    canDeactivate(
        component: ExtensionContainerComponent,
        currentRoute: ActivatedRouteSnapshot,
        currentState: RouterStateSnapshot,
        nextState: RouterStateSnapshot
    ): Promise<boolean> {
        return this.extensionContainerService.extensionContainerComponent.canDeactivate(
            component,
            currentRoute,
            currentState,
            nextState
        );
    }
}
