import { observable } from 'mobx';
import { createPage } from '<%= subLevelPath %>../../common/weapp-page'
import { concatClassList, px2rpx } from '<%= subLevelPath %>../../common/style'
import { app } from '<%= subLevelPath %>../../app/weapps-api'
import { <%= pageName %> as handlers } from '../../app/handlers'
<%= importor.lifecycle? `import lifecyle from '../../lowcode/${pageName}/lifecycle'` : "const lifecyle = {}" %>
<%= importor.state? `import state from '../../lowcode/${pageName}/state'` : "const state = {}" %>
<%= importor.computed? `import computed from '../../lowcode/${pageName}/computed'` : "const computed = {}" %>
import { $page } from './api'

const $app = new Proxy({}, { get: function(obj, prop){ return app[prop] }});

const widgetProps = <%= stringifyObj(widgetProps, {depth: null}) %>
/** widget event listeners **/
const evtListeners = {<% Object.entries(eventHanlders).map(([handlerName, listeners])=>{%>
  <%= handlerName%>: [
    <%listeners.map(l=> { %>{
      key: '<%= l.key %>',
      handler: <% if (l.type === 'rematch') {%> handlers.<%= l.handler %> <%} else if (l.type == 'material') {%> function(...args) { return require('../../materials/<%= l.handlerModule %>/actions/<%= l.handler %>/index').default(...args) } <%} else if (l.type == 'inline') {%> function({event, lists, forItems, $context}){const $for = forItems; const $index=lists?.[0]?.currentIndex; return <%= l.handler %> } <%} else {%> <%= l.handler %> <%} %>,
      data: <%= stringifyObj(l.data, {depth: null}) %>,
      boundData: {<% Object.entries(l.boundData).map(([prop, bindMeta])=>{%>'<%= prop %>':($page, lists, forItems, event, $context) => {<%= bindMeta.imports %> return (
        <%= bindMeta.expression === '' ? 'undefined': bindMeta.expression %>
      )},
        <%}) %>}
    },<%})%>
  ],<%})%>
}

const dataBinds = {<% Object.entries(dataBinds).map(([id, widgetBinds])=>{%>
  <%= id %>: { <% Object.entries(widgetBinds).map(([prop, bindMeta]) => { %>
    "<%= prop %>": function ($page, lists, forItems, event, $context) {<%= bindMeta.imports %>  return (
      <%= bindMeta.expression === '' ? 'undefined': bindMeta.expression %>
    ); },<% }) %>
  },<%}) %>
}

createPage('<%= pageName %>', '<%= pageUUID %>', widgetProps, {}, lifecyle, state, computed, evtListeners, dataBinds, app, handlers, $page, <%= pageAttributes?JSON.stringify(pageAttributes):'{}' %>)
