/*!
Copyright (c) 2016 Dominik Moritz & Roy Popiolek 2021

This file is part of the leaflet locate control. It is licensed under the MIT license.
You can find the project at: https://github.com/domoritz/leaflet-locatecontrol
*/
(function (factory, window) {
    if (typeof define === 'function' && define.amd) {
        define(['leaflet'], factory);
    } else if (typeof exports === 'object') {
        if (typeof window !== 'undefined' && window.L) {
            module.exports = factory(L);
        } else {
            module.exports = factory(require('leaflet'));
        }
    }
    if (typeof window !== 'undefined' && window.L){
        window.L.Control.Zoom2Start = factory(L);
    }
} 

(function (L) {

    var LocateControl = L.Control.extend({
        options: {
            position: 'topleft',
            icon: 'fa fa-bullseye',
            iconLoading: 'fa fa-spinner fa-spin',
            iconElementTag: 'span',
            textElementTag: 'small',
            circlePadding: [0, 0],
            metric: true,
            /**
             * This callback can be used in case you would like to override button creation behavior.
             * This is useful for DOM manipulation frameworks such as angular etc.
             * This function should return an object with HtmlElement for the button (link property) and the icon (icon property).
             */
            createButtonCallback: function (container, options) {
                var link = L.DomUtil.create('a', 'leaflet-bar-part leaflet-bar-part-single', container);
                link.title = options.strings.title;
                link.role = 'button';
                link.href = '#';
                var icon = L.DomUtil.create(options.iconElementTag, options.icon, link);

                if (options.strings.text !== undefined) {
                    var text = L.DomUtil.create(options.textElementTag, 'leaflet-locate-text', link);
                    text.textContent = options.strings.text;
		            link.classList.add('leaflet-locate-text-active');
                    link.parentNode.style.display = "flex";
                    if (options.icon.length > 0) {
                        icon.classList.add('leaflet-locate-icon');
                    }
                }
                
                return { link: link, icon: icon };
            }

        },


        /**
         * Add control to map. Returns the container for the control.
         */
        onAdd: function (map) {
            var container = L.DomUtil.create('div',
                'leaflet-control-locate leaflet-bar leaflet-control');
            this._container = container;
            this._map = map;
            this._layer = this.options.layer || new L.LayerGroup();
            this._layer.addTo(map);
            this._event = undefined;
            this._compassHeading = null;
            this._prevBounds = null;

            var linkAndIcon = this.options.createButtonCallback(container, this.options);
            this._link = linkAndIcon.link;
            this._icon = linkAndIcon.icon;

            L.DomEvent.on(
              this._link,
              "click",
              function (ev) {
                L.DomEvent.stopPropagation(ev);
                L.DomEvent.preventDefault(ev);
                this._onClick();
              },
              this
            ).on(this._link, "dblclick", L.DomEvent.stopPropagation);

            this._resetVariables();

            this._map.on('unload', this._unload, this);

            return container;
        },

        /**
         * This method is called when the user clicks on the control.
         */
        _onClick: function() {
			map.flyTo([originlat, originlon], originzoom);
        },


        /**
         * This method launches the location engine.
         * It is called before the marker is updated,
         * event if it does not mean that the event will be ready.
         *
         * Override it if you want to add more functionalities.
         * It should set the this._active to true and do nothing if
         * this._active is true.
         */

        /**
         * Remove the marker from map.
         */
        _removeMarker: function() {
            this._layer.clearLayers();
            this._marker = undefined;
            this._circle = undefined;
        },

        /**
         * Unload the plugin and all event listeners.
         * Kind of the opposite of onAdd.
         */
        _unload: function() {
            this.stop();
            this._map.off('unload', this._unload, this);
        },





        /**
         * Reinitializes state variables.
         */
        _resetVariables: function() {
            // whether locate is active or not
            this._active = false;

            // true if the control was clicked for the first time
            // we need this so we can pan and zoom once we have the location
            this._justClicked = false;

            // true if the user has panned the map after clicking the control
            this._userPanned = false;

            // true if the user has zoomed the map after clicking the control
            this._userZoomed = false;
        }
    });

    L.control.zoom2start = function (options) {
        return new L.Control.Zoom2Start(options);
    };

    return LocateControl;
}, window));