{
  "script": "function click(args){var params=args.toJS()\nvar button=params.button;var state=params.state;var maxLength=9;if(button.type=='clear'){if(state.isC){state.num='0';if(state.second){state.num2=state.num;}\nelse{state.num1=state.num;}\nstate.isC=false;}\nelse{state={};}}\nelse if(button.type=='sign'){if(state.clear){state.num='0';state.clear=false;}\nstate.num=state.num.startsWith('-')?state.num.substring(1):'-'+state.num;}\nelse if(button.type=='percent'){state.num=state.num/100;if(state.second){state.num2=state.num;}\nelse{state.num1=state.num;}}\nelse if(button.type=='equal'){if(!state.op){return;}\nif(!state.num2){state.num2=state.num1;}\nstate.num=eval((state.num1||state.num)+state.op+(state.num2));state.num1=null;state.clear=true;state.second=false;}\nelse if(button.type=='point'){if(state.num.includes('.')||state.num.length>=maxLength){return;}\nstate.num+='.';}\nelse if(button.op){if(state.num1&&state.num2&&state.second){state.num1=state.num=eval((state.num1||state.num)+state.op+(state.num2||state.num));}\nstate.num1=state.num;state.second=true;state.op=button.op;state.clear=true;}\nelse{state.num=(state.num=='0'||state.clear)?button.text:(state.num=='-0'?'-':(state.num||''))+button.text;if(state.num.replace('.','').length>maxLength){return;}\nif(state.second){state.num2=state.num;}\nelse{state.num1=state.num;}\nstate.clear=false;}\nif(state.num&&state.num!='0'){state.isC=true;}\nstate.showNum=state.num?String(state.num).replace('-','–').replace(/\\d+/,function(s){return s.replace(/\\d+?(?=(\\d{3})+\\b)/g,function(s){return s+',';});}):'0';params.mistitem.updateState(block('NSDictionary *',function(oldState){return state}))}\nglobal.export(click)",
  "state": {
    "num": "0"
  },
  "styles": {
    "button": {
      "font-size": 40,
      "font-style": "thin",
      "background-image": {
        "normal": "#d5d5d5",
        "highlighted": "#e8d5d5d5"
      }
    },
    "gray": {
      "font-size": 27,
      "background-image": {
        "normal": "#c8c8c8",
        "highlighted": "#e8c8c8c8"
      }
    },
    "orange": {
      "font-size": 52,
      "background-image": {
        "normal": "#f78c2a",
        "highlighted": "#e8f78c2a"
      },
      "title-color": "white"
    }
  },
  "layout": {
    "vars": {
      "rows": [
        {
          "buttons": [
            { "text": "${state.isC ? 'C' : 'AC'}", "type": "clear", "style": "gray" },
            { "text": "⁺∕₋", "type": "sign", "style": "gray" },
            { "text": "％", "type": "percent", "style": "gray" },
            { "text": "÷", "op": "/", "style": "orange" }
          ]
        },
        {
          "buttons": [
            { "text": "7" },
            { "text": "8" },
            { "text": "9" },
            { "text": "×", "op": "*", "style": "orange" }
          ]
        },
        {
          "buttons": [
            { "text": "4" },
            { "text": "5" },
            { "text": "6" },
            { "text": "−", "op": "-", "style": "orange" }
          ]
        },
        {
          "buttons": [
            { "text": "1" },
            { "text": "2" },
            { "text": "3" },
            { "text": "+", "op": "+", "style": "orange" }
          ]
        },
        {
          "buttons": [
            { "text": "0" },
            { "text": ".", "type": "point" },
            { "text": "=", "type": "equal", "style": "orange" }
          ]
        }
      ]
    },
    "style": {
      "direction": "vertical",
      "background-color": "black",
      "height": "${_height_ - 44 - screen.statusBarHeight - screen.safeArea.bottom}"
    },
    "children": [
      {
        "type": "text",
        "style": {
          "text": "${state.showNum ?: '0'}",
          "adjusts-font-size": true,
          "font-size": 98,
          "font-style": "ultra-light",
          "flex-grow": 1,
          "vertical-alignment": "bottom",
          "alignment": "right",
          "padding-right": 10,
          "background-color": "#202020",
          "color": "white"
        }
      },
      {
        "vars": [
          {
            "lineWidth": "${1 / UIScreen.mainScreen.scale}"
          },
          {
            "buttonSize": "${floor((_width_ - lineWidth * 3) / 4)}"
          }
        ],
        "style": {
          "direction": "vertical",
          "align-self": "center",
          "spacing": "${lineWidth}"
        },
        "children": [
          {
            "repeat": "${rows}",
            "style": {
              "spacing": "${lineWidth}"
            },
            "children": [
              {
                "repeat": "${_item_.buttons}",
                "class": "button ${_item_.style}",
                "type": "button",
                "style": {
                  "title": "${_item_.text}",
                  "height": "${buttonSize}",
                  "width": "${buttonSize}",
                  "flex-grow": "${_item_.text == '0' ? 1 : 0}"
                },
                "on-tap": {
                  "js-click": {
                    "state": "${state}",
                    "button": "${_item_}",
                    "mistitem": "${_mistitem_}"
                  }
                }
              }
            ]
          }
        ]
      }
    ]
  }
}