import { nothing } from 'lit';
import { directive, Directive, PartType } from 'lit/directive.js';
class DataAttrDirective extends Directive {
    rendered;
    constructor(partInfo) {
        super(partInfo);
        if (partInfo.type !== PartType.ELEMENT) {
            throw new Error('dataAttr() can only be used on an element');
        }
    }
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    render(attributes) {
        return nothing;
    }
    update(part, props) {
        const element = part.element;
        const map = props[0];
        if (!map) {
            this.clearRendered(element);
            return nothing;
        }
        const keys = Object.keys(map);
        if (!keys.length) {
            this.clearRendered(element);
            return nothing;
        }
        this.clearRendered(element, keys);
        this.rendered = map;
        const { dataset } = element;
        keys.forEach((key) => {
            const value = String(map[key]);
            if (dataset[key] === value) {
                return;
            }
            dataset[key] = value;
        });
        return nothing;
    }
    clearRendered(element, keep = []) {
        const { rendered } = this;
        if (!rendered) {
            return;
        }
        const keys = Object.keys(rendered);
        if (!keys.length) {
            return;
        }
        const { dataset } = element;
        keys.forEach((key) => {
            if (keep.includes(key)) {
                return;
            }
            // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
            delete dataset[key];
        });
    }
}
export const dataAttr = directive(DataAttrDirective);
//# sourceMappingURL=data-attr.js.map