global.$=require("jquery"),global._=require("lodash"),global._WM_APP_PROPERTIES={};const cheerio=require("cheerio"),{readFile:readFile,isMobileProject:isMobileProject}=require("./wm-utils"),nodePolyfill=require("node-window-polyfill").register(!1),getRequiredScripts=e=>{const t={wmCalendar:["./node_modules/fullcalendar/dist/fullcalendar.min.js"],wmChart:["./node_modules/d3/d3.min.js","./node_modules/@wavemaker.com/nvd3/build/nv.d3.min.js"],wmChips:["./node_modules/jquery-ui/ui/widgets/sortable.js"],wmList:["./node_modules/jquery-ui/ui/widgets/droppable.js","./node_modules/jquery-ui/ui/widgets/sortable.js"],wmRichtexteditor:["./node_modules/summernote/dist/summernote-lite.min.js"],wmTable:[global._WM_PACKAGE_PATH+"/scripts/datatable/datatable.js","./node_modules/jquery-ui/ui/widgets/resizable.js"]};let r=[];return _.each(t,(t,o)=>{e("["+o+"]").length>0&&(r=r.concat(t))}),r=r.map(e=>e.replace(/^(\.\/)/,"").replace(/\//g,"-")),_.uniq(r)},includesCustomElement=e=>{return cheerio.load(e)('[html-custom-element="true"]').length},getPageContents=async(e,t,r,o,i)=>{try{const l=`${t}/${r}`,s=await readFile(`${l}.html`,"utf8"),a=await readFile(`${l}.js`,"utf8"),n=await readFile(`${l}.css`,"utf8"),d=await readFile(`${l}.variables.json`,"utf8");let u;global._WM_APP_PROPERTIES=e,window=window||{},window._WM_APP_PROPERTIES=e;const m=(u=isMobileProject(e)?require("../dependencies/transpilation-mobile.cjs"):require("../dependencies/transpilation-web.cjs")).transpile(s),c=cheerio.load(m.markup),p=getRequiredScripts(c);return p.length>0&&(m.markup=m.markup.replace(">",` scripts-to-load="${p.join(",")}">`)),{markup:m.markup,script:a,styles:u.scopeComponentStyles(o,i,n),variables:d,requiredWMComponents:m.requiredWMComponents.filter(t=>!t.platformType||t.platformType===e.platformType),hasCustomElement:includesCustomElement(s)}}catch(e){throw console.error(`error occurred in transpilation of ${r}`,e),e}};module.exports={getPageContents:getPageContents};