"use strict";
/*
 * Tencent is pleased to support the open source community by making
 * Hippy available.
 *
 * Copyright (C) 2017-2019 THL A29 Limited, a Tencent company.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.routeApp = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const cors_1 = tslib_1.__importDefault(require("@koa/cors"));
const koa_static_cache_1 = tslib_1.__importDefault(require("koa-static-cache"));
const koa_static_1 = tslib_1.__importDefault(require("koa-static"));
const koa_bodyparser_1 = tslib_1.__importDefault(require("koa-bodyparser"));
const file_1 = require("@debug-server-next/utils/file");
const log_1 = require("@debug-server-next/utils/log");
const debug_targets_1 = require("@debug-server-next/router/debug-targets");
const config_1 = require("@debug-server-next/config");
const log = new log_1.Logger('router');
const routeApp = (app) => {
    const { static: staticPath, entry } = global.debugAppArgv;
    app.use(koa_bodyparser_1.default());
    app.use(cors_1.default());
    app.use(async (ctx, next) => {
        var _a, _b;
        try {
            await next();
        }
        catch (e) {
            log.error('koa error: %s', (_a = e) === null || _a === void 0 ? void 0 : _a.stack);
            return (ctx.body = (_b = e) === null || _b === void 0 ? void 0 : _b.stack);
        }
    });
    const debugTargetsRouter = debug_targets_1.getDebugTargetsRouter();
    app.use(debugTargetsRouter.routes()).use(debugTargetsRouter.allowedMethods());
    const defaultStaticOption = {
        buffer: false,
        dynamic: true,
        preload: false,
    };
    // chrome devtools
    app.use(koa_static_cache_1.default(file_1.getModuleFolder('@hippy/chrome-devtools', 'out/Release/gen'), {
        ...defaultStaticOption,
        maxAge: 60 * 60,
    }));
    // vue devtools
    app.use(koa_static_cache_1.default(file_1.getModuleFolder('@hippy/vue-devtools', 'dist'), {
        ...defaultStaticOption,
        maxAge: 60 * 60,
    }));
    // react devtools
    app.use(koa_static_cache_1.default(file_1.getModuleFolder('@hippy/react-devtools', 'dist'), {
        ...defaultStaticOption,
        maxAge: 60 * 60,
    }));
    // chrome devtools extensions
    app.use(koa_static_cache_1.default(file_1.getModuleFolder('@hippy/chrome-devtools-extensions', 'dist'), {
        ...defaultStaticOption,
        maxAge: 60 * 60,
    }));
    // hmr resources
    app.use(koa_static_1.default(config_1.config.hmrStaticPath));
    // bundle resources
    let servePath;
    if (staticPath)
        servePath = path_1.default.resolve(staticPath);
    else
        servePath = path_1.default.resolve(path_1.default.dirname(entry));
    log.info(`serve bundle: ${entry}; serve folder: ${servePath}`);
    app.use(koa_static_1.default(servePath));
};
exports.routeApp = routeApp;
