import * as i0 from '@angular/core';
import { Injectable, Inject, Directive, Input, HostListener, NgModule } from '@angular/core';
import { DOCUMENT, CommonModule } from '@angular/common';

/** @dynamic */
class ScrollToService {
    constructor(document) {
        this.document = document;
    }
    /**
     * Scroll to element
     */
    scrollTo({ element, duration = 500, offset = 0, mainView = 'body' }) {
        const el = this._getElem(element);
        if (!el) {
            throw new Error(`Element [${element}] for isScrollTo not found`);
        }
        this._scrollToElement(el, duration, offset, mainView);
    }
    /**
     * Get HTMLElement
     */
    _getElem(element) {
        return typeof element === 'string'
            ? this.document.querySelector(element)
            : element instanceof HTMLElement
                ? element
                : null;
    }
    /**
     * Do scroll to the element
     */
    _scrollToElement(el, duration, offset, mainView) {
        if (el) {
            // just suppress lint warning
            offset = offset;
            // todo: temp code, without animation
            el.scrollIntoView();
            // if(!el.offsetTop){
            if (offset) {
                // todo: need to fix this 'hardcoded' selection
                this.document.querySelector(mainView).scrollTop -= offset;
            }
            // }
            // --------------------------------------------------
            // this._doScrolling(el.offsetTop + offset, duration);
        }
        else {
            throw new Error('Element for isScrollTo not found');
        }
    }
    static { this.ɵfac = function ScrollToService_Factory(t) { return new (t || ScrollToService)(i0.ɵɵinject(DOCUMENT)); }; }
    static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: ScrollToService, factory: ScrollToService.ɵfac, providedIn: 'root' }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ScrollToService, [{
        type: Injectable,
        args: [{
                providedIn: 'root'
            }]
    }], () => [{ type: Document, decorators: [{
                type: Inject,
                args: [DOCUMENT]
            }] }], null); })();

class ScrollToDirective {
    constructor(scrollToService) {
        this.scrollToService = scrollToService;
    }
    ngOnInit() {
        this.scrollDuration = !this.scrollDuration ? 500 : this.scrollDuration;
        this.scrollOffset = !this.scrollOffset ? 0 : this.scrollOffset;
    }
    onMouseClick() {
        if (this.fusionScrollTo) {
            this.scrollToService.scrollTo({
                element: this.fusionScrollTo,
                duration: this.scrollDuration,
                offset: this.scrollOffset,
                mainView: this.scrollMainView
            });
        }
    }
    static { this.ɵfac = function ScrollToDirective_Factory(t) { return new (t || ScrollToDirective)(i0.ɵɵdirectiveInject(ScrollToService)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: ScrollToDirective, selectors: [["", "fusionScrollTo", ""]], hostBindings: function ScrollToDirective_HostBindings(rf, ctx) { if (rf & 1) {
            i0.ɵɵlistener("mouseup", function ScrollToDirective_mouseup_HostBindingHandler() { return ctx.onMouseClick(); });
        } }, inputs: { fusionScrollTo: "fusionScrollTo", scrollDuration: "scrollDuration", scrollOffset: "scrollOffset", scrollMainView: "scrollMainView" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ScrollToDirective, [{
        type: Directive,
        args: [{
                selector: '[fusionScrollTo]'
            }]
    }], () => [{ type: ScrollToService }], { fusionScrollTo: [{
            type: Input
        }], scrollDuration: [{
            type: Input
        }], scrollOffset: [{
            type: Input
        }], scrollMainView: [{
            type: Input
        }], onMouseClick: [{
            type: HostListener,
            args: ['mouseup']
        }] }); })();

class ScrollToModule {
    static { this.ɵfac = function ScrollToModule_Factory(t) { return new (t || ScrollToModule)(); }; }
    static { this.ɵmod = /*@__PURE__*/ i0.ɵɵdefineNgModule({ type: ScrollToModule }); }
    static { this.ɵinj = /*@__PURE__*/ i0.ɵɵdefineInjector({ imports: [CommonModule] }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ScrollToModule, [{
        type: NgModule,
        args: [{
                declarations: [ScrollToDirective],
                exports: [ScrollToDirective],
                imports: [CommonModule]
            }]
    }], null, null); })();
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && i0.ɵɵsetNgModuleScope(ScrollToModule, { declarations: [ScrollToDirective], imports: [CommonModule], exports: [ScrollToDirective] }); })();

/**
 * Generated bundle index. Do not edit.
 */

export { ScrollToDirective, ScrollToModule };
//# sourceMappingURL=ironsource-fusion-ui-directives-scroll-to.mjs.map
