global.$=require("jquery"),global._=require("lodash"),global._WM_APP_PROPERTIES={};const cheerio=require("cheerio"),{generatePageExpressions:generatePageExpressions}=require("./expr-parser-utils"),{readFile:readFile,isMobileProject:isMobileProject}=require("./wm-utils"),{config:config}=require("yargs"),nodePolyfill=require("node-window-polyfill").register(!1),getRequiredScripts=e=>{const t={wmCalendar:["./node_modules/fullcalendar/main.min.js"],wmChart:["./node_modules/d3/dist/d3.min.js","./node_modules/@wavemaker/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 o=[];return _.each(t,(t,r)=>{e("["+r+"]").length>0&&(o=o.concat(t))}),o=o.map(e=>e.replace(/^(\.\/)/,"").replace(/\//g,"-").replace(/@/g,"")),_.uniq(o)},includesCustomElement=e=>{return cheerio.load(e)('[html-custom-element="true"]').length},_decodeURIComponent=e=>decodeURIComponent(e.replace(/\+/g," ")),getCustomWidgetPageContents=async(e,t)=>{try{jsonObject=e[t],styles=_decodeURIComponent(jsonObject.styles),variables=_decodeURIComponent(jsonObject.variables),markup=_decodeURIComponent(jsonObject.markup),script=_decodeURIComponent(jsonObject.script),configJson=_decodeURIComponent(jsonObject.config);let o=require("../dependencies/transpilation-web.cjs");const r=o.transpile(markup);return{markup:r.markup,script:script,styles:o.scopeComponentStyles(t,"WIDGET",styles),variables:variables,requiredWMComponents:r.requiredWMComponents.filter(e=>!e.platformType||"APPLICATION"===e.platformType),config:configJson}}catch(e){throw console.error(`error occurred in transpilation of ${t}`,e),e}},getPageContents=async(e,t,o,r,s,n,i)=>{try{const a=`${t}/${o}`,l=await readFile(`${a}.html`,"utf8");let d,u,c,p="{}";"LAYOUT"!==s&&(d=await readFile(`${a}.js`,"utf8"),u=await readFile(`${a}.css`,"utf8"),p=await readFile(`${a}.variables.json`,"utf8")),global._WM_APP_PROPERTIES=e,window=window||{},window._WM_APP_PROPERTIES=e;const m=(c=isMobileProject(e)?require("../dependencies/transpilation-mobile.cjs"):require("../dependencies/transpilation-web.cjs")).transpile(l),g=cheerio.load(m.markup),j=getRequiredScripts(g);return j.length>0&&(m.markup=m.markup.replace(">",` scripts-to-load="${j.join(",")}">`)),{markup:m.markup,script:d,styles:c.scopeComponentStyles(r,s,u),variables:p,expressions:i?generatePageExpressions(m.markup,p,n):"",requiredWMComponents:m.requiredWMComponents.filter(t=>!t.platformType||t.platformType===e.platformType),hasCustomElement:includesCustomElement(l)}}catch(e){throw console.error(`error occurred in transpilation of ${o}`,e),e}};module.exports={getPageContents:getPageContents,getCustomWidgetPageContents:getCustomWidgetPageContents};