SET apply = CASE COALESCE($apply, '') WHEN 'Apply' THEN TRUE ELSE FALSE END;
SET clear = CASE COALESCE($clear, '') WHEN 'Clear' THEN TRUE ELSE FALSE END;
SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/cookie.sql') AS properties;
SET products = sqlpage.cookie('products')::json;
SET categories = sqlpage.cookie('categories')::json;
SET groups = sqlpage.cookie('groups')::json;
SET tags = sqlpage.cookie('tags')::json;
SET start = sqlpage.cookie('start');
SET end = sqlpage.cookie('end');
SET unit_volume = sqlpage.cookie('unit_volume');
SET unit_mass = sqlpage.cookie('unit_mass');
SET unit_count = sqlpage.cookie('unit_count')::json;
SET log_scale = sqlpage.cookie('log_scale');

SET unit_volume = (CASE
  WHEN $unit_volume = '' OR ($unit_volume IS NULL OR $unit_volume = '')
  THEN 'L'
  ELSE $unit_volume
END);
SET unit_mass = (CASE
  WHEN $unit_mass = '' OR ($unit_mass IS NULL OR $unit_mass = '')
  THEN 'kg'
  ELSE $unit_mass
END);
SET start = (CASE
  WHEN $start = '' OR $start IS NULL
  THEN to_char(now()-'30 days'::interval, 'YYYY-MM-DD')
  ELSE $start
END);
SET end = CASE
  WHEN $end = '' OR $end IS NULL
  THEN to_char(now(), 'YYYY-MM-DD')
  ELSE $end
END;
SET log_scale = CASE
  WHEN lower($log_scale) = 'true'
  THEN 'true'
  ELSE 'false'
END;

SET page = CASE
  WHEN $title IN (
    'Products', 'Categories', 'Groups', 'Tags', 'Transactions',
    'Trend', 'Volume'
  )
  THEN $title||'.sql'
  ELSE NULL
END;

SELECT 'redirect' AS component
, '/grocery/internal/404.sql?title='||COALESCE($title, '') AS link
WHERE ($page IS NULL OR $page = '')
;

SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/tags.sql') AS properties
WHERE $apply::bool OR $clear::bool;
SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/transactions.sql') AS properties
WHERE $apply::bool OR $clear::bool;
SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/products.sql') AS properties
WHERE $apply::bool OR $clear::bool;
SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/units.sql') AS properties
WHERE $apply::bool OR $clear::bool;

SET filter_options = (
  SELECT json_agg(json_build_object('name', k, 'options', o))
  FROM (
    SELECT o.k||'[]', json_agg(
      json_build_object('label', v, 'value', v, 'selected', s)
      ORDER BY v)
    FROM (
      SELECT DISTINCT o.k, CASE o.k
          WHEN 'products' THEN product
          WHEN 'categories' THEN category
          WHEN 'groups' THEN "group"
        END, v.k IS NOT NULL --, v.v
      FROM sqlpage_products
      CROSS JOIN UNNEST(ARRAY['products', 'categories', 'groups']) o(k)
      LEFT JOIN json_each_text(sqlpage.variables()::json) v(k,v)
      ON v.k = o.k
      AND (CASE o.k
        WHEN 'products' THEN product
        WHEN 'categories' THEN category
        WHEN 'groups' THEN "group"
      END) IN (
        SELECT j.v#>>'{}' FROM json_array_elements(v.v::json) j(v)
      )
    ) AS o(k, v, s)
    WHERE v IS NOT NULL
    GROUP BY o.k
    UNION ALL
    SELECT o.k||'[]', json_agg(
      json_build_object('label', v, 'value', v, 'selected', s)
      ORDER BY v)
    FROM (
      SELECT DISTINCT 'tags', "Name", v.k IS NOT NULL
      FROM sqlpage_tags
      LEFT JOIN json_each_text(sqlpage.variables()::json) v(k,v)
      ON v.k = 'tags' AND "Name" IN (
        SELECT j.v#>>'{}' FROM json_array_elements(v.v::json) j(v)
      )
    ) AS o(k, v, s)
    WHERE v IS NOT NULL
    GROUP BY o.k
    UNION ALL
    SELECT CASE o.k
      WHEN 'count' THEN 'unit_'||o.k||'[]'
      ELSE 'unit_'||o.k END, json_agg(
        json_build_object('label', v, 'value', v, 'selected', s)
        ORDER BY v)
    FROM (
      SELECT DISTINCT o.k, name, v.k IS NOT NULL
      FROM sqlpage_units
      CROSS JOIN UNNEST(ARRAY['volume', 'mass', 'count']) o(k)
      LEFT JOIN json_each_text(sqlpage.variables()::json) v(k,v)
      ON v.k = 'unit_'||o.k AND (CASE o.k
        WHEN 'count' THEN name IN (
          SELECT j.v#>>'{}' FROM json_array_elements(v.v::json) j(v)
        )
        ELSE name = v.v END)
      WHERE lower(type) = o.k
    ) AS o(k, v, s)
    WHERE v IS NOT NULL
    GROUP BY o.k
  ) q(k, o)
);

SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/theme.sql') AS properties;
SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/nav.sql') AS properties;

SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/pages/'||$page) AS properties;