import{C as $,D as _,E as v,e as M,f as C,h as m,i as k,j as L,k as W,z as D}from"./chunk-DQXXLIFV.js";import{_ as S,da as A,f as w,fd as G,ia as O,r as N}from"./chunk-LU6Q5ONP.js";import{a as T,b as E,k as h}from"./chunk-5VMFMKJR.js";k();S();C();var a=class n{static{this.COLORS={PRIMARY_BLUE:"#4285F4",ORANGE:"#FF8C00",ORANGE_BORDER:"#FF4500",WHITE:"white",BLACK:"#333",LIGHT_GRAY:"#eee"}}static{this.POLYLINE={STROKE_COLOR:n.COLORS.PRIMARY_BLUE,STROKE_OPACITY:.8,STROKE_WEIGHT:3}}static{this.MARKER={CIRCLE_SIZE:24,SPACING:4,CIRCLE_RADIUS:10,FONT_SIZE:10,FONT_WEIGHT:"bold",LABEL_COLOR:n.COLORS.WHITE,BORDER_WIDTH:2}}static{this.INFO_WINDOW={MAX_WIDTH:"300px",FONT_FAMILY:"var(--paw-font-family)",MARGIN_BOTTOM:"10px",PADDING:"5px",BORDER_BOTTOM:`1px solid ${n.COLORS.LIGHT_GRAY}`,TITLE_MARGIN:"0 0 5px 0",TITLE_COLOR:n.COLORS.BLACK,TEXT_MARGIN:"2px 0",NO_DATA_TITLE_MARGIN:"0 0 8px 0",NO_DATA_TEXT_MARGIN:"4px 0"}}static{this.MAP_BOUNDS={PADDING:{TOP:50,RIGHT:50,BOTTOM:50,LEFT:50}}}static{this.DEFAULT_LOCATION={LAT:46.1512,LNG:14.9955}}static{this.EMOJIS={START_FLAG:"\u{1F3F3}\uFE0F",LOCATION_PIN:"\u{1F4CD}"}}static getPolylineOptions(){return{strokeColor:n.POLYLINE.STROKE_COLOR,strokeOpacity:n.POLYLINE.STROKE_OPACITY,strokeWeight:n.POLYLINE.STROKE_WEIGHT}}static getDirectionsRendererOptions(){return{suppressMarkers:!0,polylineOptions:n.getPolylineOptions()}}static getMarkerLabelOptions(c=n.MARKER.LABEL_COLOR,t=n.MARKER.FONT_WEIGHT){return{color:c,fontWeight:t}}static getInfoWindowContent(c){return`
      <div style="font-family: ${n.INFO_WINDOW.FONT_FAMILY}; max-width: ${n.INFO_WINDOW.MAX_WIDTH};">
        ${c}
      </div>
    `}static getMarkerInfoContent(c,t,e){let o=e.notApplicable,i=t?`
      <p style="margin: ${n.INFO_WINDOW.TEXT_MARGIN};"><strong>${e.purpose}:</strong> ${t.purpose||o}</p>
      <p style="margin: ${n.INFO_WINDOW.TEXT_MARGIN};"><strong>${e.distance}:</strong> ${t.kmString||o}</p>
      <p style="margin: ${n.INFO_WINDOW.TEXT_MARGIN};"><strong>${e.date}:</strong> ${t.dateHourFrom?new Date(t.dateHourFrom).toLocaleDateString():o}</p>
      <p style="margin: ${n.INFO_WINDOW.TEXT_MARGIN};"><strong>${e.post}:</strong> ${t.post||o}</p>
    `:"";return`
      <div style="margin-bottom: ${n.INFO_WINDOW.MARGIN_BOTTOM}; padding: ${n.INFO_WINDOW.PADDING}; border-bottom: ${n.INFO_WINDOW.BORDER_BOTTOM};">
        <h4 style="margin: ${n.INFO_WINDOW.TITLE_MARGIN}; color: ${n.INFO_WINDOW.TITLE_COLOR};">${c}</h4>
        ${i}
      </div>
    `}static getNoDataInfoContent(){return`
      <h4 style="margin: ${n.INFO_WINDOW.NO_DATA_TITLE_MARGIN}; color: ${n.INFO_WINDOW.TITLE_COLOR};">No Travel Data Available</h4>
      <p style="margin: ${n.INFO_WINDOW.NO_DATA_TEXT_MARGIN};">No distance data found for this travel order.</p>
    `}static getMapBoundsPadding(){return n.MAP_BOUNDS.PADDING}static getDefaultLocation(){return{lat:n.DEFAULT_LOCATION.LAT,lng:n.DEFAULT_LOCATION.LNG}}static getGroupMarkerSvg(c,t,e){return`
      <svg width="${t}" height="${e}" viewBox="0 0 ${t} ${e}" fill="none" xmlns="http://www.w3.org/2000/svg">
        ${c.map((o,i)=>{let s=i*n.MARKER.CIRCLE_SIZE+i*n.MARKER.SPACING+n.MARKER.CIRCLE_SIZE/2,r=n.MARKER.CIRCLE_SIZE/2;return`<circle cx="${s}" cy="${r}" r="${n.MARKER.CIRCLE_RADIUS}" fill="${n.COLORS.ORANGE}" stroke="${n.COLORS.ORANGE_BORDER}" stroke-width="${n.MARKER.BORDER_WIDTH}"/>
                    <text x="${s}" y="${r+2}" text-anchor="middle" fill="${n.MARKER.LABEL_COLOR}" font-size="${n.MARKER.FONT_SIZE}" font-weight="${n.MARKER.FONT_WEIGHT}">${o.label}</text>`}).join("")}
      </svg>
    `}};var I=class{constructor(c,t,e,o){this.translate=o,this.isGroupMarker=!0,this.markers=[],this.map=c,this.position=t,this.markers.push(e),this.createOverlay(),this.addClickListeners()}createOverlay(){let c=this.markers.length,t=a.MARKER.CIRCLE_SIZE,e=a.MARKER.SPACING,o=c*t+(c-1)*e,i=t,s=a.getGroupMarkerSvg(this.markers,o,i);this.overlay=new window.google.maps.Marker({position:this.position,map:this.map,icon:{url:"data:image/svg+xml;charset=UTF-8,"+encodeURIComponent(s),scaledSize:new window.google.maps.Size(o,i),anchor:new window.google.maps.Point(o/2,i/2)},title:this.markers.map(r=>r.title).join(", ")})}addMarkerToGroup(c){this.markers.push(c),this.updateOverlay()}updateOverlay(){this.overlay&&this.overlay.setMap(null),this.createOverlay(),this.addClickListeners()}addClickListeners(){this.infoWindow||(this.infoWindow=new window.google.maps.InfoWindow),this.overlay.addListener("click",()=>{let c={purpose:this.translate.instant("Purpose"),distance:this.translate.instant("Distance"),date:this.translate.instant("Date"),post:this.translate.instant("Post"),notApplicable:this.translate.instant("N/A")},t=this.markers.map(e=>a.getMarkerInfoContent(e.title,e.distance,c)).join("");this.infoWindow.setContent(a.getInfoWindowContent(t)),this.infoWindow.open(this.map,this.overlay)})}getPosition(){return this.position}setMap(c){this.overlay&&this.overlay.setMap(c)}};G();W();v();$();var Z=(()=>{class n{static{this.MAP_RESIZE_DELAY_MS=100}static{this.MAP_INIT_DELAY_MS=100}static{this.MARKER_POSITION_TOLERANCE=1e-5}constructor(t,e,o){this.apiService=t,this.localeService=e,this.translate=o,this.apiLoaded=!1,this.loadPromise=null,this.isGoogleApiKeyValid=!0,this.apiKeyMissing$=new w,this.apiKeyInvalid$=new w}fetchGoogleApiKey(){return h(this,null,function*(){try{let t=yield N(this.apiService.getText(m.subject.googleApiKey));return t||this.apiKeyMissing$.next(),t||""}catch(t){return console.error("Failed to fetch Google API key",t),""}})}loadGoogleMapsScript(t){return this.apiLoaded?Promise.resolve(!0):this.loadPromise?this.loadPromise:(this.loadPromise=this.executeScriptLoad(t).then(e=>(e||(this.loadPromise=null),e)),this.loadPromise)}executeScriptLoad(t){return window.google&&window.google.maps?(this.apiLoaded=!0,Promise.resolve(!0)):new Promise(e=>{this.attachAuthFailureHook();let o=document.createElement("script"),i=t?`key=${encodeURIComponent(t)}`:"",s=this.localeService.getGoogleMapsLanguageCode();o.src=`https://maps.googleapis.com/maps/api/js?${i}&libraries=drawing,geometry,places&language=${s}&v=weekly`,o.async=!0,o.defer=!0,o.onload=()=>{this.apiLoaded=!0,e(!0)},o.onerror=()=>{e(!1)},document.head.appendChild(o)})}attachAuthFailureHook(){let t=window.gm_authFailure;window.gm_authFailure=()=>{this.isGoogleApiKeyValid=!1,this.apiKeyInvalid$.next(),typeof t=="function"&&t()}}loadGoogleMaps(){return h(this,null,function*(){if(this.apiLoaded)return!0;let t=yield this.fetchGoogleApiKey();if(!t)return!1;let e=yield this.loadGoogleMapsScript(t);return e&&this.validateApiKey(),e})}ensureLoadedForPlaces(){return h(this,null,function*(){return window.google?.maps?.places?.Autocomplete?!0:(yield this.loadGoogleMaps())?(yield new Promise(e=>setTimeout(e,n.MAP_INIT_DELAY_MS)),!!window.google?.maps?.places?.Autocomplete):!1})}isApiKeyValid(){return this.isGoogleApiKeyValid}calculateDistance(t,e){return h(this,null,function*(){return new Promise(o=>{let i=new window.google.maps.DirectionsService,s={origin:t,destination:e,travelMode:window.google.maps.DirectionsTravelMode.DRIVING};i.route(s,(r,d)=>{if(d!==window.google.maps.DirectionsStatus.OK){o(0);return}let g=r.routes[0].legs[0];if(!g){o(0);return}let l=M.strToNum(g.distance.text);o(l??0)})})})}validateApiKey(){return new Promise(t=>{try{let e=new window.google.maps.DirectionsService,o={origin:"0,0",destination:"1,1",travelMode:window.google.maps.DirectionsTravelMode.DRIVING};e.route(o,(i,s)=>{let r=[window.google.maps.DirectionsStatus.REQUEST_DENIED,window.google.maps.DirectionsStatus.INVALID_REQUEST,window.google.maps.DirectionsStatus.OVER_QUERY_LIMIT];this.isGoogleApiKeyValid=!r.includes(s),this.isGoogleApiKeyValid||this.apiKeyInvalid$.next(),t(this.isGoogleApiKeyValid)})}catch{this.isGoogleApiKeyValid=!1,t(!1)}})}createMap(t,e){return!t||!window.google?.maps?null:new window.google.maps.Map(t,e)}calculateAndDisplayRoute(t,e,o,i,s){let r=!!s;if(!r&&e.length===0){i?.();return}if(r&&!s.start&&e.length===0){i?.();return}let d=new window.google.maps.DirectionsService,g=new window.google.maps.DirectionsRenderer(a.getDirectionsRendererOptions());g.setMap(t),t.directionsRenderer=g;let l=this.buildRouteRequest(e,s);d.route(l,(u,p)=>{if(p==="OK"){g.setDirections(u),this.addMarkersToRoute(t,u.routes[0],o,s),this.fitMapToRouteBounds(t,u.routes[0]),i?.();return}let R=r?[s.start,...e,s.end].filter(Boolean):e;this.fallbackToIndividualGeocoding(t,R,o,i)})}buildRouteRequest(t,e){let o=!!e,i=o&&e.start||t[0],s=o&&e.end||t[t.length-1],r=o?t.map(d=>({location:d,stopover:!0})):t.length>2?t.slice(1,-1).map(d=>({location:d,stopover:!0})):[];return{origin:i,destination:s,travelMode:window.google.maps.TravelMode.DRIVING,optimizeWaypoints:!1,waypoints:r}}fitMapToRouteBounds(t,e){let o=new window.google.maps.LatLngBounds;e.legs.forEach(i=>{o.extend(i.start_location),o.extend(i.end_location)}),t.fitBounds(o,a.getMapBoundsPadding()),setTimeout(()=>{window.google.maps.event.trigger(t,"resize")},n.MAP_RESIZE_DELAY_MS)}addMarkersToRoute(t,e,o,i){if(e.legs.length!==0){if(i){this.addHeaderMarkers(t,e,o,i);return}this.addRouteMarkersWithoutHeader(t,e,o)}}addHeaderMarkers(t,e,o,i){let s=e.legs[0];if(this.addMarker(t,s.start_location,i.start||this.translate.instant("StartLocation"),a.EMOJIS.START_FLAG,null,!0),e.legs.forEach((g,l)=>{if(l>=o.length)return;let u=o[l]?.ordinalNumber||l+1;this.addMarker(t,g.end_location,o[l]?.distance?.displayNode||o[l]?.distance?.node||`${this.translate.instant("Waypoint")} ${u}`,`${u}`,o[l]?.distance,!1)}),!i.end)return;let r=e.legs[e.legs.length-1],d=o.length>0?o[o.length-1]?.ordinalNumber||o.length:0;this.addMarker(t,r.end_location,i.end||this.translate.instant("EndLocation"),`${d+1}`,null,!1)}addRouteMarkersWithoutHeader(t,e,o){let i=e.legs[0];this.addMarker(t,i.start_location,o[0]?.distance?.displayNode||o[0]?.distance?.node||this.translate.instant("Start"),a.EMOJIS.START_FLAG,o[0]?.distance,!0),e.legs.forEach((s,r)=>{r>=o.length-1||this.addMarker(t,s.end_location,o[r+1]?.distance?.displayNode||o[r+1]?.distance?.node||`${this.translate.instant("Waypoint")} ${r+1}`,`${r+1}`,o[r+1]?.distance,!1)})}addMarker(t,e,o,i,s,r=!1){let{lat:d,lng:g}=this.positionToCoords(e),l={title:o,label:i,distance:s,isFirstPoint:r},u=this.findExistingGroupAt(t,d,g);if(u)return u.addMarkerToGroup(l),u;let p=new I(t,e,l,this.translate);return t.markers||(t.markers=[]),t.markers.push(p),p}positionToCoords(t){return{lat:typeof t.lat=="function"?t.lat():t.lat,lng:typeof t.lng=="function"?t.lng():t.lng}}findExistingGroupAt(t,e,o){if(!t.markers||t.markers.length===0)return null;let i=n.MARKER_POSITION_TOLERANCE;for(let s of t.markers){if(!s.isGroupMarker)continue;let r=s.getPosition();if(!r)continue;let{lat:d,lng:g}=this.positionToCoords(r);if(Math.abs(e-d)<i&&Math.abs(o-g)<i)return s}return null}fallbackToIndividualGeocoding(t,e,o,i){if(e.length===0){i?.();return}let s=new window.google.maps.LatLngBounds,r=[],d=0;e.forEach((g,l)=>{new window.google.maps.Geocoder().geocode({address:g},(p,R)=>{if(d++,R==="OK"&&p[0]){let f=p[0].geometry.location;s.extend(f);let y=this.addMarker(t,f,o[l]?.distance?.displayNode||o[l]?.distance?.node||g,l===0?a.EMOJIS.START_FLAG:`${l}`,o[l]?.distance,l===0);r.push(y)}d===e.length&&(this.drawRouteLines(t,r,[]),r.length>0&&t.fitBounds(s,a.getMapBoundsPadding()),i?.())})})}drawRouteLines(t,e,o){for(let i=0;i<e.length-1;i++){let s=new window.google.maps.Polyline(E(T({path:[e[i].getPosition(),e[i+1].getPosition()],geodesic:!0},a.getPolylineOptions()),{map:t}));o.push(s)}t.polylines||(t.polylines=[]),t.polylines.push(...o)}clearMap(t){if(!t)return;(t.markers||[]).forEach(i=>{i.setMap(null)}),(t.polylines||[]).forEach(i=>{i.setMap(null)}),t.directionsRenderer&&t.directionsRenderer.setMap(null),t.overlayMapTypes?.clear(),t.markers=[],t.polylines=[],t.directionsRenderer=null}addDefaultMarker(t){if(!t)return;let e=a.getDefaultLocation(),o=new window.google.maps.Marker({position:e,map:t,title:this.translate.instant("DefaultLocation"),label:T({text:a.EMOJIS.LOCATION_PIN},a.getMarkerLabelOptions())}),i=new window.google.maps.InfoWindow({content:a.getInfoWindowContent(this.buildNoDataInfoContent())});o.addListener("click",()=>{i.open(t,o)})}buildNoDataInfoContent(){let t=this.translate.instant("NoTravelDataAvailable"),e=this.translate.instant("NoDistanceDataFound");return`
      <h4 style="margin: ${a.INFO_WINDOW.NO_DATA_TITLE_MARGIN}; color: ${a.INFO_WINDOW.TITLE_COLOR};">${t}</h4>
      <p style="margin: ${a.INFO_WINDOW.NO_DATA_TEXT_MARGIN};">${e}</p>
    `}reverseGeocode(t,e){return h(this,null,function*(){if(!window.google?.maps)throw new Error("Google Maps not loaded");let o=yield this.geocode({location:{lat:t,lng:e}});if(!o)throw new Error("Geocoding failed");return o[0].formatted_address})}reverseGeocodeForPostalCode(t,e){return h(this,null,function*(){if(!window.google?.maps)return null;let o=yield this.geocode({location:{lat:t,lng:e}});return o&&o[0].address_components?.find(s=>s.types.includes("postal_code")||s.types.includes("postal_code_prefix"))?.long_name||null})}geocode(t){return new Promise(e=>{new window.google.maps.Geocoder().geocode(t,(i,s)=>{e(s==="OK"&&i?.length?i:null)})})}static{this.\u0275fac=function(e){return new(e||n)(O(L),O(_),O(D))}}static{this.\u0275prov=A({token:n,factory:n.\u0275fac,providedIn:"root"})}}return n})();export{Z as a};
