import BaseEvent from"./events/Event.js";import BaseObject from"./Object.js";import EventType from"./events/EventType.js";import{circular as circularPolygon}from"./geom/Polygon.js";import{get as getProjection,getTransformFromProjections,identityTransform}from"./proj.js";import{toRadians}from"./math.js";const Property={ACCURACY:"accuracy",ACCURACY_GEOMETRY:"accuracyGeometry",ALTITUDE:"altitude",ALTITUDE_ACCURACY:"altitudeAccuracy",HEADING:"heading",POSITION:"position",PROJECTION:"projection",SPEED:"speed",TRACKING:"tracking",TRACKING_OPTIONS:"trackingOptions"};class GeolocationError extends BaseEvent{constructor(t){super(EventType.ERROR),this.code=t.code,this.message=t.message}}class Geolocation extends BaseObject{constructor(t){super(),this.on,this.once,this.un;t=t||{};this.position_=null,this.transform_=identityTransform,this.watchId_=void 0,this.addChangeListener(Property.PROJECTION,this.handleProjectionChanged_),this.addChangeListener(Property.TRACKING,this.handleTrackingChanged_),void 0!==t.projection&&this.setProjection(t.projection),void 0!==t.trackingOptions&&this.setTrackingOptions(t.trackingOptions),this.setTracking(void 0!==t.tracking&&t.tracking)}disposeInternal(){this.setTracking(!1),super.disposeInternal()}handleProjectionChanged_(){var t=this.getProjection();t&&(this.transform_=getTransformFromProjections(getProjection("EPSG:4326"),t),this.position_&&this.set(Property.POSITION,this.transform_(this.position_)))}handleTrackingChanged_(){var t;"geolocation"in navigator&&((t=this.getTracking())&&void 0===this.watchId_?this.watchId_=navigator.geolocation.watchPosition(this.positionChange_.bind(this),this.positionError_.bind(this),this.getTrackingOptions()):t||void 0===this.watchId_||(navigator.geolocation.clearWatch(this.watchId_),this.watchId_=void 0))}positionChange_(t){var t=t.coords,e=(this.set(Property.ACCURACY,t.accuracy),this.set(Property.ALTITUDE,null===t.altitude?void 0:t.altitude),this.set(Property.ALTITUDE_ACCURACY,null===t.altitudeAccuracy?void 0:t.altitudeAccuracy),this.set(Property.HEADING,null===t.heading?void 0:toRadians(t.heading)),this.position_?(this.position_[0]=t.longitude,this.position_[1]=t.latitude):this.position_=[t.longitude,t.latitude],this.transform_(this.position_));this.set(Property.POSITION,e),this.set(Property.SPEED,null===t.speed?void 0:t.speed);const i=circularPolygon(this.position_,t.accuracy);i.applyTransform(this.transform_),this.set(Property.ACCURACY_GEOMETRY,i),this.changed()}positionError_(t){this.dispatchEvent(new GeolocationError(t))}getAccuracy(){return this.get(Property.ACCURACY)}getAccuracyGeometry(){return this.get(Property.ACCURACY_GEOMETRY)||null}getAltitude(){return this.get(Property.ALTITUDE)}getAltitudeAccuracy(){return this.get(Property.ALTITUDE_ACCURACY)}getHeading(){return this.get(Property.HEADING)}getPosition(){return this.get(Property.POSITION)}getProjection(){return this.get(Property.PROJECTION)}getSpeed(){return this.get(Property.SPEED)}getTracking(){return this.get(Property.TRACKING)}getTrackingOptions(){return this.get(Property.TRACKING_OPTIONS)}setProjection(t){this.set(Property.PROJECTION,getProjection(t))}setTracking(t){this.set(Property.TRACKING,t)}setTrackingOptions(t){this.set(Property.TRACKING_OPTIONS,t)}}export default Geolocation;