// #region Imports
import { noChange } from 'lit';
import { PartType, directive } from 'lit/directive.js';
import { RendererServiceLocator } from '../../../Dom/Renderer';
import { DirectiveBase } from '../Abstracts/DirectiveBase';
import { Spacing } from '../../Types/Spacing';
import { Size } from '../../Types/Size';
/**
 * @private
 */
class SpacerDirective extends DirectiveBase {
    // #region Fields
    _options;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `SpacerDirective` class.
     *
     * @public
     */
    constructor(args) {
        super(args);
        if (args.type !== PartType.ELEMENT) {
            throw new Error('The `spacer` directive can only be used on elements.');
        }
        this._options = undefined;
    }
    // #endregion
    /**
     * @public
     */
    render(options) {
        this._options = options;
        return noChange;
    }
    /**
     * @public
     * @override
     */
    update(part, [options]) {
        const element = part.element;
        this._options = options;
        const { multiplier = 8, size = Size.Medium, thickness = [], invert = false, multiplierValidator = (m) => !((m / 4) % 1) } = options;
        if (!multiplierValidator(multiplier)) {
            throw new Error('Invalid space multiplier.');
        }
        const getDensityAsNumber = (s) => {
            switch (s) {
                case Size.Tiny:
                    return 1;
                case Size.Small:
                    return 2;
                case Size.Medium:
                    return 3;
                case Size.Large:
                    return 4;
                case Size.Giant:
                    return 5;
                default:
                    return 3;
            }
        };
        const parseSize = (sz) => `${sz}px`;
        const parseThickness = (t) => {
            if (!t)
                return [];
            if (Array.isArray(t))
                return t;
            return [t];
        };
        const tryGetThickness = (keys, thicknessArr) => {
            let sizePx = multiplier * getDensityAsNumber(size);
            sizePx = invert ? -Math.abs(sizePx) : Math.abs(sizePx);
            for (const k of keys) {
                if (thicknessArr.includes(k))
                    return parseSize(sizePx);
            }
            return '0px';
        };
        const thicknessArr = parseThickness(thickness);
        RendererServiceLocator.current.setStyle(element, 'margin-top', tryGetThickness([Spacing.Top, Spacing.Vertical, Spacing.All], thicknessArr));
        RendererServiceLocator.current.setStyle(element, 'margin-right', tryGetThickness([Spacing.Right, Spacing.Horizontal, Spacing.All], thicknessArr));
        RendererServiceLocator.current.setStyle(element, 'margin-bottom', tryGetThickness([Spacing.Bottom, Spacing.Vertical, Spacing.All], thicknessArr));
        RendererServiceLocator.current.setStyle(element, 'margin-left', tryGetThickness([Spacing.Left, Spacing.Horizontal, Spacing.All], thicknessArr));
        return this.render(options);
    }
}
/**
 * @public
 */
export const spacer = directive(SpacerDirective);
//# sourceMappingURL=SpacerDirective.js.map