/*! phoscon-app 2024-10-07 */

function getStateId(id){if(!id)throw new Error("parameter is missing - getStateId()");return id=(id=id.toLowerCase()).replace("_","-")}export function createMachineContent(pages,c,install_c){const result={},initOn={};if(pages.length<1)throw new Error("expected pages to be not empty");return pages.forEach(page=>{const state=getStateId(page.id),on={},eventHandlers=page.eventHandler||[];if(page.id===c.PAGE_VENDOR&&eventHandlers.forEach(handler=>{if(checkIcCapable(handler,install_c)){handler.actions.find(h=>"CALL_PAGE"===h.type).data=c.PAGE_INSTALLCODE_METHOD}}),eventHandlers.length<1)throw new Error("expected eventHandler to be not empty");eventHandlers.forEach(handler=>{if(!handler.event)throw"event is missing page "+page.id;if(on[handler.event]={actions:(context,event)=>{if(handler.cond&&event.device){let cond=handler.cond,sw=event.device;var btnEv=sw.get("buttonevent");let modelid=sw.get("modelid");if(cond.buttonevent&&cond.buttonevent!==btnEv)return;var foundId=void 0;if(cond.modelids&&(foundId=cond.modelids.find(m=>-1!==modelid.indexOf(m))),!foundId)return}event.type&&"DEVICE_UPDATED"===event.type&&context.updatedTimer&&(context.updatedTimer,clearTimeout(context.updatedTimer)),(event.type&&"DEVICE_ADDED"===event.type&&context.scanTimer||event.type&&"DEVICE_UPDATED"===event.type&&context.scanTimer)&&(clearTimeout(context.scanTimer),context.scanTimer=null),handler.actions.forEach(action=>{const eventArg={...action,...handler.cond},{service:{machine:{options:options}}}=context;"function"==typeof options.actions[action.type]&&options.actions[action.type](context,eventArg)})}},!handler.actions)throw new Error("expected actions to be defined");if(handler.actions.length<1)throw new Error("expected actions to be not empty");handler.actions.filter(x=>x.type===c.CALL_PAGE).forEach(action=>{if(!action.data)throw"action data for CALL_PAGE is missing page "+page.id;const nextPage=pages.find(p=>p.id==action.data);if(!nextPage)throw"couldn't find page "+action.data;on[action.data]=getStateId(nextPage.id),on[action.cond]=handler.cond,initOn[action.data]=getStateId(nextPage.id)})}),on[c.PAGE_INIT]=getStateId(c.PAGE_INIT),page.id!==c.PAGE_INIT&&(on[page.id]=state),result[state]={on:on,onEntry:[c.CALL_ON_ENTRY]}}),result["page-init"].on=Object.assign(result["page-init"].on,initOn),result};function checkIcCapable(handler,install_c){let t=handler.actions.find(h=>"CALL_PAGE"===h.type);if(!t)return!1;if(!install_c)return;let identifier=install_c.VendorMacIdentifier,vendor=t.data.split("_")[1],res=!1;return identifier.forEach(i=>{let v=i.vendor.toUpperCase();v&&vendor===v.split("-").join("")&&(res=!0)}),res}