/*
 * ******************************************************************
 * Copyright (c) 2025 Broadcom. All Rights Reserved.
 * Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc.
 * and/or its subsidiaries.
 * ******************************************************************
 */

import { Component, OnDestroy } from "@angular/core";
import { VmActionExtensionConsumer, ActionItem } from "@vcf/ng-hyper-plugin-framework";
import { map, mergeMap, of, Subscription } from "rxjs";

@Component({
    standalone: false,
    selector: "vm-actions",
    templateUrl: "./vm-actions-demo.component.html",
    providers: [VmActionExtensionConsumer],
    styleUrls: [
        "./vm-actions-demo.component.scss",
    ]
})
export class VmActionsDemoComponent implements OnDestroy {
    extensionActions: ActionItem[] = [];
    extensionActionsVmSubscription: Subscription | undefined;

    constructor(
        public vmActionExtensionConsumer: VmActionExtensionConsumer,
    ) {
        this.extensionActionsVmSubscription = of(
            {
                name: "vm1",
                uid: "vm1:id"
            }
        )
        .pipe(
            mergeMap((vm) => {
                const actions = this.vmActionExtensionConsumer.getActionsForEntity(of(vm.uid));
                return actions;
            }),
            map((actions) => {
                this.extensionActions = actions;
            }),
        )
        .subscribe();
    }

    ngOnDestroy(): void {
        if (this.extensionActionsVmSubscription) {
            this.extensionActionsVmSubscription.unsubscribe();
        }
    }
}