import * as i0 from '@angular/core';
import { EventEmitter, Directive, HostListener, Input, Output, ViewChild } from '@angular/core';
import { BehaviorSubject, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { isNullOrUndefined } from '@ironsource/fusion-ui/utils';

const _c0 = ["contentRef"];
class ButtonBaseComponent {
    /** @internal */
    onClick($event) {
        this.onclick.emit($event);
    }
    /**
     * Add button icon (IconData = string | Icon)
     * @param value
     * @internal
     */
    set icon(value) {
        if (!isNullOrUndefined(value)) {
            this.iconData = value;
            this.iconName = typeof this.iconData === 'string' ? this.iconData : this.iconData.iconName;
            this.setIconState(!!this.iconName);
        }
        else {
            this.setIconState(false);
        }
    }
    /**
     * Set disable state
     * @param value
     * @internal
     */
    set disabled(value) {
        this.isDisabled = value;
        this.setDisableState(value);
    }
    /**
     * Set loading state
     * @param value
     * @internal
     */
    set loading(value) {
        this.isLoading$.next(value);
    }
    get loading() {
        return this.isLoading$.getValue();
    }
    /**
     * Set button as link
     * @param value
     * @internal
     */
    set link(value) {
        this.isLink = value;
        this.setLinkButtonState(this.isLink);
    }
    constructor(injector, element, renderer) {
        this.element = element;
        this.renderer = renderer;
        /**
         * @internal
         */
        this.onclick = new EventEmitter();
        this.isLoading$ = new BehaviorSubject(false);
        this.onDestroy$ = new Subject();
    }
    ngOnInit() {
        this.isLoading$.asObservable().pipe(takeUntil(this.onDestroy$)).subscribe(this.setLoadingState.bind(this));
    }
    ngAfterViewInit() {
        this.projectContent = !!this.contentRef?.nativeElement?.innerText?.trim();
        this.setHostClass(this.projectContent, 'fu-with-content');
    }
    ngOnDestroy() {
        this.onDestroy$.next();
        this.onDestroy$.complete();
    }
    setHostClass(add, className) {
        const method = add ? 'addClass' : 'removeClass';
        this.renderer[method](this.element.nativeElement, className);
    }
    setHostAttribute(add, attributeName, attributeValue) {
        const method = add ? 'setAttribute' : 'removeAttribute';
        this.renderer[method](this.element.nativeElement, attributeName, add ? attributeValue : null);
    }
    setDisableState(disabling) {
        this.setHostClass(disabling, 'disabled');
        this.setHostAttribute(disabling, 'disabled', 'true');
    }
    setLoadingState(loading) {
        this.setHostClass(loading, 'fu-with-loading');
        if (!this.isDisabled) {
            this.setDisableState(loading);
        }
    }
    setIconState(hasIcon) {
        this.setHostClass(hasIcon, 'fu-iconed');
    }
    setLinkButtonState(isLinkButton) {
        this.setHostClass(isLinkButton, 'fu-link-button');
    }
    static { this.ɵfac = function ButtonBaseComponent_Factory(t) { return new (t || ButtonBaseComponent)(i0.ɵɵdirectiveInject(i0.Injector), i0.ɵɵdirectiveInject(i0.ElementRef), i0.ɵɵdirectiveInject(i0.Renderer2)); }; }
    static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: ButtonBaseComponent, viewQuery: function ButtonBaseComponent_Query(rf, ctx) { if (rf & 1) {
            i0.ɵɵviewQuery(_c0, 5);
        } if (rf & 2) {
            let _t;
            i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.contentRef = _t.first);
        } }, hostBindings: function ButtonBaseComponent_HostBindings(rf, ctx) { if (rf & 1) {
            i0.ɵɵlistener("click", function ButtonBaseComponent_click_HostBindingHandler($event) { return ctx.onClick($event); });
        } }, inputs: { icon: "icon", disabled: "disabled", loading: "loading", link: "link" }, outputs: { onclick: "onclick" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(ButtonBaseComponent, [{
        type: Directive
    }], () => [{ type: i0.Injector }, { type: i0.ElementRef }, { type: i0.Renderer2 }], { onClick: [{
            type: HostListener,
            args: ['click', ['$event']]
        }], icon: [{
            type: Input
        }], disabled: [{
            type: Input
        }], loading: [{
            type: Input
        }], link: [{
            type: Input
        }], onclick: [{
            type: Output
        }], contentRef: [{
            type: ViewChild,
            args: ['contentRef']
        }] }); })();

const BUTTON_OPTIONS_DEFAULT_VALUE = {
    svgOptions: { assetsPath: '' }
};

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

export { BUTTON_OPTIONS_DEFAULT_VALUE, ButtonBaseComponent };
//# sourceMappingURL=ironsource-fusion-ui-components-button-common-base.mjs.map
