import { Directive, HostBinding, Input, OnInit, OnDestroy, inject } from '@angular/core';

import { AccordionDirective } from './accordion.directive';

@Directive({
    selector: '[appAccordionLink]',
    standalone: true,
})
export class AccordionLinkDirective implements OnInit, OnDestroy {
    @Input()
    public group!: string;

    @HostBinding('class.selected')
    @Input()
    get selected(): boolean {
        return this._selected;
    }

    set selected(value: boolean) {
        this._selected = value;
        if (value) {
            this.nav.closeOtherLinks(this);
        }
    }

    protected _selected: boolean = false;
    protected nav: AccordionDirective;

    constructor() {
        const nav = inject<AccordionDirective>(AccordionDirective);

        this.nav = nav;
    }

    ngOnInit() {
        this.nav.addLink(this);
    }

    ngOnDestroy() {
        this.nav.removeGroup(this);
    }

    toggle() {
        this.selected = !this.selected;
    }
}
