require('./index.less');

import fetch from 'zero-net/fetch';
import hashRouter from 'zero-routing/hashRouter';
import menus from './menus';
import renderer from './renderer';
import win from 'zero-lang/global';
import xmind from 'xmind/index-browser';
import instanceCache from './storage/instance';

fetch(win.location.pathname + '?raw')
    .then(function (res) {
        return res.arrayBuffer();
    })
    .then(function (data) {
        var instance = xmind.open(data);
        instanceCache.set('current', instance);

        // fulfill menus
        menus.fulfill();

        // routing!
        hashRouter({
            '/:sheet': function (sheetIndex) {
                renderer.render(sheetIndex);
            },

            '/:sheet/:topic': function (sheetIndex, topicId) {
                renderer
                    .render(sheetIndex)
                    .highlight(topicId);
            },

            '*': function () {
                hashRouter('/0'); // render the first sheet by default
            },
        });

        win.pageLoading.hideOverlay();
    });
