import { BodyScrollService } from '@shared/services/body-scroll.service';
import { ScrollToDirective } from './scroll-to.directive';
let element: HTMLElement;
let container: HTMLElement;
let $window: Window & typeof globalThis;
let bodyScroll: BodyScrollService;

describe('ScrollToDirective', () => {
    let directive: ScrollToDirective;
    beforeEach(() => {
        jasmine.clock().install();
        bodyScroll = {
            scrollToElement: jasmine.createSpy(),
        } as any;
        element = {} as any;
        directive = new ScrollToDirective({ nativeElement: element }, bodyScroll);
    });

    afterEach(() => {
        jasmine.clock().uninstall();
    });

    it('should scroll inside body', () => {
        directive.shouldScroll = true;
        directive.ngOnChanges();
        jasmine.clock().tick(1);

        expect(bodyScroll.scrollToElement).toHaveBeenCalled();
    });

    it('should not scroll inside body', () => {
        directive.shouldScroll = false;
        directive.ngOnChanges();

        expect(bodyScroll.scrollToElement).not.toHaveBeenCalled();
    });

    it('should scroll within parent', () => {
        (element as any).offsetTop = 20;
        container = {
            clientHeight: 10,
            scrollTo: jasmine
                .createSpy('scrollTo')
                .withArgs({
                    top: 20 - 10 / 2,
                    left: 0,
                    behavior: 'smooth',
                })
                .and.callThrough(),
        } as any;

        directive = new ScrollToDirective({ nativeElement: element }, bodyScroll, { element: { nativeElement: container } });
        directive.shouldScroll = true;
        directive.ngOnChanges();
        jasmine.clock().tick(1);

        expect(bodyScroll.scrollToElement).not.toHaveBeenCalled();
        expect(container.scrollTo).toHaveBeenCalled();
    });

    it('should not scroll within parent', () => {
        container = {
            scrollTo: jasmine.createSpy(),
        } as any;
        directive = new ScrollToDirective({ nativeElement: element }, bodyScroll, { element: { nativeElement: container } });
        directive.shouldScroll = false;
        directive.ngOnChanges();
        jasmine.clock().tick(1);

        expect(bodyScroll.scrollToElement).not.toHaveBeenCalled();
        expect(container.scrollTo).not.toHaveBeenCalled();
    });
});
