//
//   Copyright 2020  SenX S.A.S.
//
//   This program is free software: you can redistribute it and/or modify it
//   under the terms of the GNU Affero General Public License as published
//   by the Free Software Foundation, version 3.
//
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty
//   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU Affero General Public License for more details.
//
//   You should have received a copy of the GNU Affero General Public License
//   along with this program. If not, see <https://www.gnu.org/licenses/>
//

15 m MSTU / MACROTTL

{
  'name' '@senx/discovery2/layout'
  'desc' 
  <'
 Generate a layout for the Discovery templating engine

 See @senx/discovery/tile
  '>
  'sig' [ [ [ 'params:MAP' ] [ 'result:MAP' ] ] ] // Signature
  'params' {
    // Signature params description
    'params' 
    <'
Map of parameters:
- **title**: Title of the dashboard - **optional**.
- **description**: Descrition of the dashboard - **optional**.
- **tiles**: List of tiles generated by `@senx/discovery/tile` - **mandatory**.
    '>
    'result' 'A layout representation'
  }
  'examples' [
    <'
{
  'title' 'Covid' // title
  'description' 'The Covid 19 dashboard' // description
  'tiles' [ // Tiles
    {  // params
      'title' 'Country' // title
      'type' 'display' // Type
      'backend' 'https://warp.senx.io/api/v0/exec' // backend
      'x' 0 'y' 0 
      'w' 2 'h' 1 
      'options' {
          'showStatus' false
          'timeMode' 'custom'
      }
      'macro' <% { 'data' [ 'France' ] } %> // macro
    }
    @senx/discovery/tile
  ]
} @senx/discovery/layout
    '>
  ]
} 'info' STORE

<%
  !$info INFO
  SAVE 'context' STORE
  <%
    // Code of the actual macro
    'params' STORE

    // test mandatory fields
    [ 'tiles' ] <% 'k' STORE
        <% $params $k CONTAINSKEY SWAP DROP ! %>
        <% $k ' is mandatory' + MSGFAIL %>
        IFT
    %> FOREACH

    <%
      [ 'field' 'defValue' ] STORE
      <% $params $field CONTAINSKEY SWAP DROP %> <% $params $field GET %> <% $defValue %> IFTE
    %> 'getOrDef' STORE

    {
        'title' 'title' '' @getOrDef
        'description' 'description' '' @getOrDef
        'tiles' 'tiles' [] @getOrDef
    }
  %>
  <% // catch any exception
    RETHROW
  %>
  <% // finally, restore the context
    $context RESTORE
  %> TRY
%>
'macro' STORE

// Unit tests

$macro
