"use strict";(()=>{var m=class{constructor(){this.events={}}on(n,e){this.events[n]||(this.events[n]=[]),this.events[n].push(e)}off(n,e){this.events[n]&&(this.events[n]=this.events[n].filter(s=>s!==e))}once(n,e){let s=t=>{this.off(n,s),e(t)};this.on(n,s)}emit(n,e){this.events[n]&&[...this.events[n]].forEach(t=>{try{t(e)}catch(r){console.error(`[EventEmitter] Error emitting event ${String(n)}:`,r)}})}removeAllListeners(n){n!==void 0?delete this.events[n]:this.events={}}listenerCount(n){return this.events[n]?.length??0}};var g="2.3.0",_=2,p=15e3,y=5e3,l=class extends m{constructor(e){super();this.iframe=null;this._isReady=!1;this._isDestroyed=!1;this._readyFailed=!1;this._readyFailedError=null;this.promiseQueue={};this._pendingCommands=[];this._pendingFireAndForget=[];this._messageListener=null;this._readyTimer=null;this.readyPayload=null;this._readyResolve=null;this._readyReject=null;this._iframeDomReadyMs=null;this._engineReadyMs=null;this._sdkCreatedAt=Date.now();let s=typeof e.container=="string"?document.getElementById(e.container):e.container;if(!s)throw new Error(`[SGSMapSDK] Cannot find container: ${e.container}`);this.container=s,this.sdkUrl=e.sdkUrl||"/h5-sdk",this.floorId=e.floorId||1,this.mapId=e.mapId||1,this.defaultTimeout=e.timeout||y,this.readyTimeoutMs=e.readyTimeout||p,this.targetOrigin=e.targetOrigin,this.targetOrigin||(this.sdkUrl.startsWith("http")?this.targetOrigin=new URL(this.sdkUrl).origin:typeof window<"u"?this.targetOrigin=window.location.origin:this.targetOrigin="*"),this._readyPromise=new Promise((t,r)=>{this._readyResolve=t,this._readyReject=r}),this._initIframeBridge()}_initIframeBridge(){let e=document.createElement("iframe"),s=new URL(this.sdkUrl,typeof window<"u"?window.location.href:"http://localhost");typeof window<"u"&&new URLSearchParams(window.location.search).forEach((r,i)=>{s.searchParams.set(i,r)}),s.searchParams.has("floorId")||s.searchParams.set("floorId",String(this.floorId)),e.src=s.toString(),e.style.cssText="width:100%;height:100%;border:none;background:transparent;",this.container.innerHTML="",this.container.appendChild(e),this.iframe=e,this._messageListener=t=>{if(this.targetOrigin!=="*"&&t.origin!==this.targetOrigin||!this.iframe||t.source!==this.iframe.contentWindow)return;let r=t.data;if(r){if(r.type==="SGS_MAP_EVENT"){let{action:i,payload:o}=r;i==="ON_POI_CLICK"?this.emit("poiClick",o):i==="ENGINE_READY"?this._onEngineReady(o):i==="ON_FLOOR_CHANGED"?this.emit("floorChanged",o):i==="ON_ROUTE_TRANSFER"?this.emit("routeTransfer",o):i==="ON_MODEL_LOADING"?this.emit("modelLoading",o):i==="ON_MODEL_READY"?this.emit("modelReady",o):i==="ON_MODEL_ERROR"?this.emit("modelError",o):i==="ON_FLOOR_BUNDLE_READY"?this.emit("floorBundleReady",o):i==="ON_ROUTE_READY"&&this.emit("routeReady",o)}if(r.type==="SGS_MAP_RESPONSE"){let{action:i,requestId:o,payload:a,error:d}=r,u=this.promiseQueue[o];if(u){if(a&&a.success!==!1)u.resolve(a);else{let c=d||{code:"ERR_FAILED",message:`${i} command failed`};u.reject(c)}delete this.promiseQueue[o]}}}},window.addEventListener("message",this._messageListener),e.onload=()=>{this._iframeDomReadyMs=Date.now()-this._sdkCreatedAt,!this._isReady&&(this._postCommand("HELLO",{protocolVersion:_,sdkVersion:g}),console.log(`[SGSMapSDK] Sent HELLO handshake (v${g}), waiting for ENGINE_READY...`))},e.onerror=t=>{console.error("[SGSMapSDK] Iframe load error",t);let r={code:"ERR_IFRAME_LOAD",message:"Failed to load map engine iframe."};this.emit("loadError",{type:"iframe_error",detail:r.message}),this._rejectReady(r)},this._readyTimer=setTimeout(()=>{if(!this._isReady&&!this._isDestroyed){let t={code:"ERR_READY_TIMEOUT",message:`ENGINE_READY not received within ${this.readyTimeoutMs}ms`};console.error(`[SGSMapSDK] ${t.message}`),this._rejectReady(t)}},this.readyTimeoutMs)}_onEngineReady(e){this._isReady||(this._isReady=!0,this.readyPayload=e,this._engineReadyMs=Date.now()-this._sdkCreatedAt,this._readyTimer&&(clearTimeout(this._readyTimer),this._readyTimer=null),this._readyResolve&&(this._readyResolve(e),this._readyResolve=null,this._readyReject=null),this.emit("ready",e),console.log("[SGSMapSDK] Handshake complete: Engine Ready received \u2705"),this._flushPendingCommands())}_rejectReady(e){this._readyFailed=!0,this._readyFailedError=e,this._readyReject&&(this._readyReject(e),this._readyResolve=null,this._readyReject=null),this._readyTimer&&(clearTimeout(this._readyTimer),this._readyTimer=null),this._rejectAllPending(e.code,e.message),this._pendingFireAndForget.splice(0)}_flushPendingCommands(){let e=this._pendingFireAndForget.splice(0);for(let r of e)this._postCommand(r.action,r.payload);let s=this._pendingCommands.splice(0);for(let r of s)this._postCommandAsyncInner(r.action,r.payload,r.timeout).then(r.resolve).catch(r.reject);let t=e.length+s.length;t>0&&console.log(`[SGSMapSDK] Flushed ${t} pending commands after ready (${e.length} fire-and-forget, ${s.length} async)`)}_rejectAllPending(e,s){Object.keys(this.promiseQueue).forEach(r=>{let i=this.promiseQueue[r];i&&i.reject({code:e,message:s}),delete this.promiseQueue[r]});let t=this._pendingCommands.splice(0);for(let r of t)r.reject({code:e,message:s})}whenReady(){return this._readyPromise}ready(){return this._readyPromise}getIsReady(){return this._isReady}getCurrentFloor(){return this.floorId}getMapId(){return this.mapId}getVersion(){return g}_generateRequestId(){return`req_sdk_${Date.now()}_${Math.floor(Math.random()*1e5)}`}_postCommand(e,s,t){if(!this.iframe||!this.iframe.contentWindow){console.error("[SGSMapSDK] SDK Iframe is not available!");return}this.iframe.contentWindow.postMessage({type:"SGS_MAP_COMMAND",action:e,requestId:t,payload:s},this.targetOrigin)}_postCommandAsync(e,s,t=this.defaultTimeout){return this._isDestroyed?Promise.reject({code:"ERR_DESTROYED",message:"SGSMapSDK has been destroyed. Cannot send commands."}):this._readyFailed?Promise.reject(this._readyFailedError||{code:"ERR_NOT_READY",message:"SDK ready failed."}):this._isReady?this._postCommandAsyncInner(e,s,t):new Promise((r,i)=>{this._pendingCommands.push({action:e,payload:s,timeout:t,resolve:r,reject:i}),console.log(`[SGSMapSDK] Command "${e}" queued (waiting for ready). Queue size: ${this._pendingCommands.length}`)})}_postCommandSafe(e,s){if(this._isDestroyed){console.warn(`[SGSMapSDK] Command "${e}" ignored: SDK destroyed.`);return}if(this._readyFailed){console.warn(`[SGSMapSDK] Command "${e}" ignored: SDK ready failed.`);return}if(!this._isReady){this._pendingFireAndForget.push({action:e,payload:s}),console.log(`[SGSMapSDK] Command "${e}" queued (fire-and-forget, waiting for ready). Queue size: ${this._pendingFireAndForget.length}`);return}this._postCommand(e,s)}_postCommandAsyncInner(e,s,t=this.defaultTimeout){let r=this._generateRequestId();return new Promise((i,o)=>{let a=setTimeout(()=>{this.promiseQueue[r]&&(this.promiseQueue[r].reject({code:"ERR_TIMEOUT",message:`Command "${e}" timed out after ${t}ms`}),delete this.promiseQueue[r])},t);this.promiseQueue[r]={resolve:d=>{clearTimeout(a),i(d)},reject:d=>{clearTimeout(a),o(d)}},this._postCommand(e,s,r)})}focusTo(e){typeof e=="string"?this._postCommandSafe("FOCUS_TO",{nodeName:e}):this._postCommandSafe("FOCUS_TO",e)}changeFloor(e,s){return this._postCommandAsync("CHANGE_FLOOR",{floorId:e},s).then(t=>(this.floorId=e,t))}addMarker(e){this._postCommandSafe("ADD_MARKER",e)}removeMarker(e){this._postCommandSafe("REMOVE_MARKER",{markerId:e})}clearMarkers(){this._postCommandSafe("CLEAR_MARKERS")}highlightPolygon(e){this._postCommandSafe("HIGHLIGHT_POLYGON",{nodeName:e})}planRoute(e,s,t,r=5e3){let i=typeof e=="string"?e:void 0,o=typeof s=="string"?s:void 0,a={};return i?a.startNode=i:a.from=e,o?a.endNode=o:a.to=s,t&&(a.options=t),this._postCommandAsync("PLAN_ROUTE",a,r)}planRouteV2(e,s,t,r=5e3){let i={from:e,to:s,options:t};return this._postCommandAsync("PLAN_ROUTE",i,r)}clearRoute(){this._postCommandSafe("CLEAR_ROUTE")}getState(){return this._postCommandAsync("GET_STATE")}resetView(){this._postCommandSafe("RESET_VIEW")}setViewMode(e){this._postCommandSafe("SET_VIEW_MODE",{mode:e})}toggleLayer(e,s,t){return this._postCommandAsync("TOGGLE_LAYER",{types:e,visible:s},t)}setVisibleLayers(e,s){return this._postCommandAsync("SET_VISIBLE_TYPES",{types:e},s)}getManifest(e){return this._postCommandAsync("GET_MANIFEST",{},e)}loadFloorBundle(e,s){return this._postCommandAsync("LOAD_FLOOR_BUNDLE",{floorId:e},s)}getFloorPois(e,s){return this._postCommandAsync("GET_FLOOR_POIS",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.pois||[])}getNavigablePlaces(e,s){return this._postCommandAsync("GET_NAVIGABLE_PLACES",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.navigablePlaces||t?.places||[])}getPois(e,s){return this._postCommandAsync("GET_POIS",{floorCode:e},s)}getSpaces(e,s){return this._postCommandAsync("GET_SPACES",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.spaces||[])}getGuideStops(e,s){return this._postCommandAsync("GET_GUIDE_STOPS",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.guideStops||[])}getGuideStopsByHall(e,s){return this._postCommandAsync("GET_GUIDE_STOPS_BY_HALL",{hallId:e},s).then(t=>Array.isArray(t)?t:t?.guideStops||[])}preloadFloor(e,s){return this._postCommandAsync("PRELOAD_FLOOR",{floorId:e},s)}getPerformanceStats(e){return this._postCommandAsync("GET_PERFORMANCE_STATS",{},e).then(s=>({sdkCreatedAt:this._sdkCreatedAt,iframeDomReadyMs:this._iframeDomReadyMs??void 0,engineReadyMs:this._engineReadyMs??void 0,...s}))}getDiagnostics(e){return this._postCommandAsync("GET_DIAGNOSTICS",{},e)}getFloorDiagnostics(e,s){return this._postCommandAsync("GET_FLOOR_DIAGNOSTICS",{floorId:e},s)}clearModelCache(){this._postCommandSafe("CLEAR_MODEL_CACHE")}getReleaseManifest(e){return console.warn("[SGSMapSDK] getReleaseManifest() is deprecated. Use getManifest() instead."),Promise.reject({code:"ERR_UNSUPPORTED",message:"getReleaseManifest() is deprecated. Use getManifest() instead."})}getModelResources(e){return console.warn("[SGSMapSDK] getModelResources() is deprecated."),Promise.reject({code:"ERR_UNSUPPORTED",message:"getModelResources() is deprecated."})}getLayerTree(e){return console.warn("[SGSMapSDK] getLayerTree() is deprecated."),Promise.reject({code:"ERR_UNSUPPORTED",message:"getLayerTree() is deprecated."})}loadMockReleaseRoute(e){return console.warn("[SGSMapSDK] loadMockReleaseRoute() is deprecated."),Promise.reject({code:"ERR_UNSUPPORTED",message:"loadMockReleaseRoute() is deprecated."})}destroy(){this._isDestroyed||(this._isDestroyed=!0,this._readyTimer&&(clearTimeout(this._readyTimer),this._readyTimer=null),this._readyReject&&(this._readyReject({code:"ERR_DESTROYED",message:"SGSMapSDK has been destroyed before ready."}),this._readyResolve=null,this._readyReject=null),this._rejectAllPending("ERR_DESTROYED","SGSMapSDK has been destroyed. Pending promises were rejected."),this.iframe&&(this.iframe.parentNode&&this.iframe.parentNode.removeChild(this.iframe),this.iframe=null),this._messageListener&&(window.removeEventListener("message",this._messageListener),this._messageListener=null),this.removeAllListeners(),this._isReady=!1,console.log("[SGSMapSDK] Destroyed successfully. Cleaned up iframe, listeners and pending queues."))}};typeof window<"u"&&(window.SGSMapSDK=l);})(); //# sourceMappingURL=index.global.js.map