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'
import lifecyle from '../../lowcode/<%= pageName %>/lifecycle'
import state from '../../lowcode/<%= pageName %>/state'
import computed from '../../lowcode/<%= pageName %>/computed'
import { $page } from './api'

const $app = app;
const context = observable({});

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}, $page){const $for = forItems; return <%= l.handler %> } <%} else {%> <%= l.handler %> <%} %>,
      data: <%= stringifyObj(l.data, {depth: null}) %>,
      boundData: {<% Object.entries(l.boundData).map(([prop, expr])=>{%>'<%= prop %>':($page, lists, forItems, event, $context) => {const $for = forItems; return (
        <%= expr === '' ? 'undefined': expr %>
      )},
        <%}) %>}
    },<%})%>
  ],<%})%>
}
const dataBinds = {<% Object.entries(dataBinds).map(([id, widgetBinds])=>{%>
  <%= id %>: { <% Object.entries(widgetBinds).map(([prop, expr]) => { %>
    "<%= prop %>": function ($page, lists, forItems, event, $context) {const $for = forItems; return (
      <%= expr === '' ? 'undefined': expr %>
    ); },<% }) %>
  },<%}) %>
}


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