const WorkflowIntegration=require("./WorkflowIntegration.node"),PLUGIN_ID="com.imdaxiong.bigbearcloud";let _resolveInstance=null,_resolvePromiseInstance=null,_projectManager=null,_project=null,_timeline=null,_mediaPool=null,_isInitialized=!1,_isInitializing=!1,_lastProjectName=null,_lastTimelineName=null;async function initialize(){if(!0===_isInitialized)return!0;if(_isInitializing)return new Promise((resolve,reject)=>{const checkInterval=setInterval(()=>{!0===_isInitialized?(clearInterval(checkInterval),resolve(!0)):!1===_isInitializing&&(clearInterval(checkInterval),reject(new Error("初始化失败")))},100)});_isInitializing=!0;try{if(!WorkflowIntegration)throw new Error("WorkflowIntegration.node 未加载，请检查文件路径");const timeoutPromise=new Promise((_,reject)=>{setTimeout(()=>reject(new Error("InitializePromise 超时 (15 秒)")),15e3)});if(!0!==await Promise.race([WorkflowIntegration.InitializePromise(PLUGIN_ID),timeoutPromise]))throw new Error("Failed to initialize communication with host");return _isInitialized=!0,_isInitializing=!1,!0}catch(e){throw _isInitializing=!1,e}}async function getResolve(){return _resolvePromiseInstance||(!0!==_isInitialized&&await initialize(),_resolvePromiseInstance=await WorkflowIntegration.GetResolvePromise(),_resolvePromiseInstance)}async function getProjectManager(){if(_projectManager)return _projectManager;const resolve=await getResolve();return _projectManager=await resolve.GetProjectManager(),_projectManager}async function getProject(forceRefresh=!1){const projectManager=await getProjectManager(),currentProject=await projectManager.GetCurrentProject();if(!currentProject)return _project=null,_lastProjectName=null,null;const currentProjectName=await currentProject.GetName();return(forceRefresh||null===_project||_lastProjectName!==currentProjectName)&&(_project=currentProject,_lastProjectName=currentProjectName,_timeline=null,_mediaPool=null),_project}async function getTimeline(forceRefresh=!1){const project=await getProject();if(!project)return _timeline=null,_lastTimelineName=null,null;const currentTimeline=await project.GetCurrentTimeline();if(!currentTimeline)return _timeline=null,_lastTimelineName=null,null;const currentTimelineName=await currentTimeline.GetName();return(forceRefresh||null===_timeline||_lastTimelineName!==currentTimelineName)&&(_timeline=currentTimeline,_lastTimelineName=currentTimelineName),_timeline}async function getMediaPool(){const project=await getProject();return project?(null===_mediaPool&&(_mediaPool=await project.GetMediaPool()),_mediaPool):(_mediaPool=null,null)}async function getCurrentPage(){const resolve=await getResolve();return await resolve.GetCurrentPage()}async function openPage(pageName){const resolve=await getResolve(),result=await resolve.OpenPage(pageName);return result&&(_timeline=null,_lastTimelineName=null),result}async function getProjectFps(){const project=await getProject();if(!project)return 24;try{const fpsStr=await project.GetSetting("timelineFrameRate");if(fpsStr)return parseFloat(fpsStr)}catch(e){}return 24}function framesToTimecode(frames,fps=25){fps<=0&&(fps=24);const totalSeconds=frames/fps,hours=Math.floor(totalSeconds/3600),minutes=Math.floor(totalSeconds%3600/60),seconds=Math.floor(totalSeconds%60),milliseconds=Math.floor(totalSeconds%1*1e3);return`${String(hours).padStart(2,"0")}:${String(minutes).padStart(2,"0")}:${String(seconds).padStart(2,"0")},${String(milliseconds).padStart(3,"0")}`}function timecodeToFrames(timecode,fps=25){if(!timecode)return 0;const parts=timecode.split(/[:,.]/);if(parts.length<4)return 0;const totalSeconds=3600*(parseInt(parts[0])||0)+60*(parseInt(parts[1])||0)+(parseInt(parts[2])||0)+(parseInt(parts[3])||0)/1e3;return Math.round(totalSeconds*fps)}async function refreshCache(){_project=null,_timeline=null,_mediaPool=null,_lastProjectName=null,_lastTimelineName=null,await getProject(!0),await getTimeline(!0),await getMediaPool()}async function cleanup(){WorkflowIntegration&&WorkflowIntegration.CleanUp&&WorkflowIntegration.CleanUp(),_resolveInstance=null,_resolvePromiseInstance=null,_projectManager=null,_project=null,_timeline=null,_mediaPool=null,_isInitialized=!1,_lastProjectName=null,_lastTimelineName=null}function isInitialized(){return!0===_isInitialized}function getCacheStatus(){return{isInitialized:_isInitialized,hasResolve:!!_resolvePromiseInstance,hasProjectManager:!!_projectManager,hasProject:!!_project,hasTimeline:!!_timeline,hasMediaPool:!!_mediaPool,lastProjectName:_lastProjectName,lastTimelineName:_lastTimelineName}}module.exports={initialize:initialize,cleanup:cleanup,isInitialized:isInitialized,getResolve:getResolve,getProjectManager:getProjectManager,getProject:getProject,getTimeline:getTimeline,getMediaPool:getMediaPool,getCurrentPage:getCurrentPage,openPage:openPage,getProjectFps:getProjectFps,framesToTimecode:framesToTimecode,timecodeToFrames:timecodeToFrames,refreshCache:refreshCache,getCacheStatus:getCacheStatus,PLUGIN_ID:PLUGIN_ID};