"use strict";

var __extends = undefined && undefined.__extends || function () {
    var extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) {
        d.__proto__ = b;
    } || function (d, b) {
        for (var p in b) {
            if (b.hasOwnProperty(p)) d[p] = b[p];
        }
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() {
            this.constructor = d;
        }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
}();
Object.defineProperty(exports, "__esModule", { value: true });
var redux_immutable_1 = require("redux-immutable");
var reducers_1 = require("../docs/reducers");
var reducers_2 = require("../sessions/reducers");
var reducers_3 = require("../sharing/reducers");
var reducers_4 = require("../history/reducers");
var immutable_1 = require("immutable");
var reducers_5 = require("../general/reducers");
var immutableMemoize_1 = require("../../components/Playground/util/immutableMemoize");
var reselect_1 = require("reselect");
var deserialize_1 = require("./deserialize");
var reducers_6 = require("../appHistory/reducers");
// import { createSelector } from 'reselect'
function getSelectedWorkspaceId(state) {
    return state.get('selectedWorkspace');
}
exports.getSelectedWorkspaceId = getSelectedWorkspaceId;
function getSelectedWorkspace(state) {
    return state.getIn(['workspaces', getSelectedWorkspaceId(state)]);
}
exports.getSelectedWorkspace = getSelectedWorkspace;
var Workspace = /** @class */function (_super) {
    __extends(Workspace, _super);
    function Workspace() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    return Workspace;
}(immutable_1.Record({
    docs: immutable_1.Map({}),
    sessions: reducers_2.makeSessionState(''),
    sharing: new reducers_3.SharingState(),
    history: immutable_1.OrderedMap(),
    general: new reducers_5.GeneralState(),
    appHistory: new reducers_6.AppHistory()
}));
exports.Workspace = Workspace;
exports.defaultSettings = {
    'general.betaUpdates': false,
    'editor.theme': 'dark',
    'editor.reuseHeaders': true,
    'request.credentials': 'omit',
    'tracing.hideTracingResponse': true
};
exports.RootState = immutable_1.Record({
    workspaces: immutable_1.Map({ '': makeWorkspace('') }),
    selectedWorkspace: '',
    settingsString: JSON.stringify(exports.defaultSettings, null, 2),
    stateInjected: false
});
var workspaceReducers = redux_immutable_1.combineReducers({
    docs: reducers_1.default,
    sessions: reducers_2.default,
    sharing: reducers_3.default,
    history: reducers_4.default,
    general: reducers_5.default,
    appHistory: reducers_6.default
});
// todo: add lru-cache later when the localStorage is full
exports.rootReducer = function (state, action) {
    if (state === void 0) {
        state = new exports.RootState();
    }
    if (action.type === 'SELECT_WORKSPACE') {
        return state.set('selectedWorkspace', action.payload.workspace);
    }
    if (action.type === 'SET_SETTINGS_STRING') {
        return state.set('settingsString', action.payload.settingsString);
    }
    if (action.type === 'INIT_STATE' && !state.stateInjected) {
        var _a = action.payload,
            workspaceId = _a.workspaceId,
            endpoint = _a.endpoint;
        if (!state.workspaces.get(workspaceId)) {
            var newState = state.setIn(['workspaces', workspaceId], makeWorkspace(endpoint));
            return newState.set('selectedWorkspace', workspaceId);
        }
        return state.set('selectedWorkspace', workspaceId);
    }
    if (action.type === 'INJECT_STATE') {
        return deserialize_1.deserializePersistedState(action.payload.state).set('stateInjected', true);
    }
    var selectedWorkspaceId = action.payload && action.payload.workspaceId ? action.payload.workspaceId : getSelectedWorkspaceId(state);
    var path = ['workspaces', selectedWorkspaceId];
    return state.setIn(path, workspaceReducers(state.getIn(path), action));
};
function makeWorkspace(endpoint) {
    var sessionState = reducers_2.makeSessionState(endpoint);
    // weird typescript error
    return new Workspace({
        docs: immutable_1.Map((_a = {}, _a[sessionState.selectedSessionId] = new reducers_1.DocsSession(), _a)),
        sessions: sessionState,
        sharing: new reducers_3.SharingState(),
        history: immutable_1.OrderedMap(),
        general: new reducers_5.GeneralState()
    });
    var _a;
}
exports.makeWorkspace = makeWorkspace;
exports.default = exports.rootReducer;
exports.getSessionCounts = immutableMemoize_1.immutableMemoize(function (state) {
    return state.workspaces.map(function (w) {
        return w.sessions.sessionCount;
    });
});
exports.getSettingsString = function (state) {
    return state.settingsString;
};
exports.getSettings = reselect_1.createSelector([exports.getSettingsString], function (settingsString) {
    try {
        return normalizeSettings(JSON.parse(settingsString));
    } catch (e) {
        return exports.defaultSettings;
    }
});
function normalizeSettings(settings) {
    var theme = settings['editor.theme'];
    if (theme !== 'dark' && theme !== 'light') {
        settings['editor.theme'] = 'dark';
    }
    return settings;
}
exports.getTheme = reselect_1.createSelector([exports.getSettings], function (s) {
    return s.theme || 'dark';
});
//# sourceMappingURL=reducers.js.map