// The MIT License (MIT)
//
// Copyright (c) 2015-2024 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import { listen, unlistenByKey } from 'ol/events';
import { CollectionEvent } from 'ol/Collection';
/**
 * Provides a featureOverlay used by the FeatureOverlayManager.
 *
 * Example of initialization:
 * const featureOverlay = new FeatureOverlay(manager, index);
 * @param {NgeoMapFeatureOverlayMgrFeatureOverlayMgr} manager The feature overlay manager.
 * @param {number} index This feature overlay's index.
 */
export class FeatureOverlay {
    constructor(manager, index) {
        this.manager_ = manager;
        this.index_ = index;
        this.listenerKeys_ = [];
    }
    /**
     * Add a feature to the feature overlay.
     * @param {OlFeature<OlGeomGeometry>} feature The feature to add.
     */
    addFeature(feature) {
        this.manager_.addFeature(feature, this.index_);
    }
    /**
     * Remove a feature from the feature overlay.
     * @param {OlFeature<OlGeomGeometry>} feature The feature to remove.
     */
    removeFeature(feature) {
        this.manager_.removeFeature(feature, this.index_);
    }
    /**
     * Check if featureOverlay has no features.
     * @returns {boolean} True if there is no features. False otherwise.
     */
    isEmpty() {
        return this.manager_.isEmpty(this.index_);
    }
    /**
     * Remove all the features from the feature overlay.
     */
    clear() {
        this.manager_.clear(this.index_);
    }
    /**
     * Configure this feature overlay with a feature collection. Features added
     * to the collection are also added to the overlay. Same for removal. If you
     * configure the feature overlay with a feature collection you will use the
     * collection to add and remove features instead of using the overlay's
     * `addFeature`, `removeFeature` and `clear` functions.
     * @param {OlCollection<OlFeature<OlGeomGeometry>>} features Feature collection.
     */
    setFeatures(features) {
        // Remove old features collection.
        this.clear();
        this.listenerKeys_.forEach(unlistenByKey);
        // Add new feature collection.
        if (features !== null) {
            features.forEach((feature) => {
                this.addFeature(feature);
            });
            // Listen collection to sync features in the manager.
            this.listenerKeys_.push(listen(features, 'add', (evt) => this.handleFeatureAdd_(evt)));
            this.listenerKeys_.push(listen(features, 'remove', (evt) => this.handleFeatureRemove_(evt)));
        }
    }
    /**
     * Set a style for the feature overlay.
     * @param {OlStyleStyle | OlStyleStyle[] | OlStyleStyleStyleFunction} style Style.
     */
    setStyle(style) {
        this.manager_.setStyle(style, this.index_);
    }
    /**
     * @param {Event|OlEventsEvent} evt Feature collection event.
     */
    handleFeatureAdd_(evt) {
        if (evt instanceof CollectionEvent) {
            const feature = evt.element;
            this.addFeature(feature);
        }
    }
    /**
     * @param {Event|OlEventsEvent} evt Feature collection event.
     */
    handleFeatureRemove_(evt) {
        if (evt instanceof CollectionEvent) {
            const feature = evt.element;
            this.removeFeature(feature);
        }
    }
}
export default FeatureOverlay;
//# sourceMappingURL=FeatureOverlay.js.map