// #region Imports
import { noChange } from 'lit';
import { IntersectionController } from '../../Controllers/IntersectionController';
import { isReactiveControllerHost } from '../../Controllers/Utils';
import { DirectiveBase, PartType, compose } from '../Abstracts/DirectiveBase';
import { emit, on } from '@breadstone/mosaik-elements';
/**
 * @private
 */
class IntersectionDirective extends DirectiveBase {
    // #region Fields
    _intersectionController;
    _intersectionEntries;
    _ref;
    // #endregion
    // #region Ctor
    /**
     * Constructs a new instance of the `IntersectionDirective` class.
     *
     * @public
     */
    constructor(args) {
        super(args);
        if (args.type !== PartType.ELEMENT) {
            throw new Error('The `intersection` directive can only be used on elements.');
        }
        if (!isReactiveControllerHost(args.element)) {
            throw new Error('The `intersection` directive can only be used on reactive controller host elements.');
        }
        this._intersectionEntries = new Array();
        this._ref = {
            register: (element) => {
                this._intersectionEntries.push(element);
            }
        };
        this._intersectionController = new IntersectionController(args.element, {
            callback: (entry) => {
                entry.filter((x) => this._intersectionEntries.includes(x.target)).forEach((e) => {
                    if (e.isIntersecting) {
                        emit(e.target, 'intersected');
                    }
                });
            }
        });
    }
    // #endregion
    // #region Methods
    /**
     * @public
     */
    render(cb) {
        // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
        if (cb) {
            cb(this._ref);
        }
        return noChange;
    }
}
/**
 * @private
 */
class IntersectionEntryDirective extends DirectiveBase {
    // #region Ctor
    /**
     * Constructs a new instance of the `IntersectionDirective` class.
     *
     * @public
     */
    constructor(args) {
        super(args);
        on(args.element, 'intersected', (_) => {
            console.log('im intersected ###############################################');
        });
    }
    render(ref) {
        ref.register(this.internals.element);
        return noChange;
    }
}
/**
 * The `intersection` directive.
 *
 * @public
 */
export const intersection = compose(IntersectionDirective);
/**
 * The `intersectionEntry` directive.
 *
 * @public
 */
export const intersectionEntry = compose(IntersectionEntryDirective);
//# sourceMappingURL=IntersectionDirective.js.map