// The MIT License (MIT)
//
// Copyright (c) 2020-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 OlStyle, { createDefaultStyle, } from 'ol/style/Style';
import OlStyleCircle from 'ol/style/Circle';
import OlStyleRegularShape from 'ol/style/RegularShape';
import OlStyleFill from 'ol/style/Fill';
import OlStyleStroke from 'ol/style/Stroke';
import { BehaviorSubject } from 'rxjs';
import 'corejs-typeahead';
/**
 * @param styleDescriptor The description of the style
 * @returns The OpenLayers stale
 */
export function buildStyle(styleDescriptor) {
    if (styleDescriptor instanceof OlStyle) {
        return styleDescriptor;
    }
    else if (!styleDescriptor) {
        return createDefaultStyle;
    }
    else if (Array.isArray(styleDescriptor)) {
        const result = [];
        for (const style of styleDescriptor) {
            result.push(buildStyle(style));
        }
        return result;
    }
    else {
        const style = {};
        Object.assign(style, styleDescriptor);
        const sd = styleDescriptor;
        if (sd.fill) {
            style.fill = new OlStyleFill(sd.fill);
        }
        if (sd.stroke) {
            style.stroke = new OlStyleStroke(sd.stroke);
        }
        if (sd.circle) {
            const circleStyle = {};
            Object.assign(circleStyle, sd.circle);
            if (sd.circle.fill) {
                circleStyle.fill = new OlStyleFill(sd.circle.fill);
            }
            if (sd.circle.stroke) {
                circleStyle.stroke = new OlStyleStroke(sd.circle.stroke);
            }
            style.image = new OlStyleCircle(circleStyle);
            // @ts-ignore
            delete style.circle;
        }
        else if (sd.regularShape) {
            const regularShapeStyle = {};
            Object.assign(regularShapeStyle, sd.regularShape);
            if (sd.regularShape.fill) {
                regularShapeStyle.fill = new OlStyleFill(sd.regularShape.fill);
            }
            if (sd.regularShape.stroke) {
                regularShapeStyle.stroke = new OlStyleStroke(sd.regularShape.stroke);
            }
            if (sd.regularShape.angle) {
                sd.regularShape.angle = (sd.regularShape.angle / 180) * Math.PI;
            }
            if (sd.regularShape.rotation) {
                sd.regularShape.rotation = (sd.regularShape.angle / 180) * Math.PI;
            }
            style.image = new OlStyleRegularShape(regularShapeStyle);
            // @ts-ignore
            delete style.regularShape;
        }
        return new OlStyle(style);
    }
}
/**
 * Object used to expose all the Configurations we get from the dynamic.json.
 *
 * If you are in an element that extends BaseElement you just need to implement
 * the ``initConfig`` to get the configuration.
 *
 * Example of usage:
 * ```js
 *    (window as any).gmfapi.store.config.getConfig().subscribe({
 *      next: (configuration: Configuration) => {
 *        if (configuration) {
 *          ...
 *        }
 *      },
 *    })
 * ```
 */
export class ConfigModel {
    constructor() {
        /**
         * The observable config's properties.
         *
         * @private
         */
        this.configuration_ = new BehaviorSubject(null);
    }
    /**
     * @returns the configuration.
     */
    getConfig() {
        return this.configuration_;
    }
    /**
     * Set the configuration.
     *
     * @param config the configuration
     */
    setConfig(config) {
        this.configuration_.next(config);
    }
}
// Export default config instantiated (to use it as a singleton).
const config = new ConfigModel();
export default config;
//# sourceMappingURL=config.js.map