"use strict";
/**
 * Copyright 2020 Baidu Inc. 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.
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable no-console */
const io_1 = __importDefault(require("./io"));
const get_port_1 = __importDefault(require("get-port"));
const mkdirp_1 = __importDefault(require("mkdirp"));
const path_1 = __importDefault(require("path"));
const rimraf_1 = __importDefault(require("rimraf"));
const child_process_1 = require("child_process");
const host = '127.0.0.1';
const publicPath = '/visualdl';
const pages = ['common', 'scalar', 'histogram', 'image', 'audio', 'graph', 'pr-curve', 'roc-curve', 'high-dimensional'];
const dataDir = path_1.default.resolve(__dirname, '../data');
async function start() {
    rimraf_1.default.sync(dataDir);
    const port = await get_port_1.default({ host });
    mkdirp_1.default.sync(dataDir);
    const io = new io_1.default(`http://${host}:${port}${publicPath}`, dataDir);
    const p = child_process_1.spawn('visualdl', [
        '--logdir',
        '.',
        '--model',
        './__model__',
        '--host',
        host,
        '--port',
        String(port),
        '--public-path',
        publicPath
    ], {
        cwd: path_1.default.resolve(__dirname, '../logs'),
        stdio: ['ignore', 'pipe', 'pipe']
    });
    p.on('error', err => console.error(err));
    const stop = () => {
        if (!p.killed) {
            p.kill('SIGINT');
        }
    };
    const check = async (data) => {
        const message = data.toString();
        if (message.startsWith('Running VisualDL')) {
            p.stdout.off('data', check);
            p.stderr.off('data', check);
            await Promise.all(pages.map(page => new Promise((resolve, reject) => {
                Promise.resolve().then(() => __importStar(require(`./${page}`))).then(data => data.default)
                    .then((worker) => worker(io).then(resolve))
                    .catch(reject);
            })));
            await io.generateMeta();
            stop();
        }
    };
    p.stdout.on('data', check);
    p.stderr.on('data', check);
    process.on('exit', stop);
}
start();
