import { DynamicItemExtensionBase } from '@anglr/dynamic';
import { isPresent } from '@jscrpt/common';
/**
 * Extension that applies common component styling to component
 */
export class ComponentStylingExtension extends DynamicItemExtensionBase {
    //######################### public methods - overrides #########################
    /**
     * @inheritdoc
     */
    onInit() {
        this._applyStyling();
    }
    /**
     * @inheritdoc
     */
    onOptionsChange() {
        this._applyStyling();
    }
    //######################### protected methods #########################
    /**
     * Applies component styling to element
     */
    _applyStyling() {
        var _a, _b;
        const options = this.options;
        const style = (_a = this.element) === null || _a === void 0 ? void 0 : _a.nativeElement.style;
        if (this.element) {
            if (isPresent(this.lastCssClass)) {
                for (const cssClass of this.lastCssClass.split(' ')) {
                    if (cssClass) {
                        this.element.nativeElement.classList.remove(cssClass);
                    }
                }
            }
            this.lastCssClass = options === null || options === void 0 ? void 0 : options.cssClass;
            if (options === null || options === void 0 ? void 0 : options.cssClass) {
                for (const cssClass of options.cssClass.split(' ')) {
                    if (cssClass) {
                        (_b = this.element) === null || _b === void 0 ? void 0 : _b.nativeElement.classList.add(cssClass);
                    }
                }
            }
        }
        if (isPresent(style)) {
            if (options === null || options === void 0 ? void 0 : options.margin) {
                if (isPresent(options.margin.bottom)) {
                    style.marginBottom = options.margin.bottom;
                }
                if (isPresent(options.margin.right)) {
                    style.marginRight = options.margin.right;
                }
                if (isPresent(options.margin.top)) {
                    style.marginTop = options.margin.top;
                }
                if (isPresent(options.margin.left)) {
                    style.marginLeft = options.margin.left;
                }
            }
            if (options === null || options === void 0 ? void 0 : options.padding) {
                if (isPresent(options.padding.bottom)) {
                    style.paddingBottom = options.padding.bottom;
                }
                if (isPresent(options.padding.right)) {
                    style.paddingRight = options.padding.right;
                }
                if (isPresent(options.padding.top)) {
                    style.paddingTop = options.padding.top;
                }
                if (isPresent(options.padding.left)) {
                    style.paddingLeft = options.padding.left;
                }
            }
            if (options === null || options === void 0 ? void 0 : options.textStyling) {
                if (isPresent(options.textStyling.fontSize)) {
                    style.fontSize = options.textStyling.fontSize;
                }
                if (isPresent(options.textStyling.fontWeight)) {
                    style.fontWeight = options.textStyling.fontWeight.toString();
                }
            }
        }
    }
}
//# sourceMappingURL=componentStyling.extension.js.map