"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (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 });
exports.NAME_SPACE = void 0;
const config_1 = __importDefault(require("./config/config"));
const logging_1 = __importDefault(require("./config/logging"));
const Xcron = __importStar(require("node-cron"));
const lib_052_master_1 = require("./lib_052_master");
exports.NAME_SPACE = "OneEtlTat Gateway";
const VERSION = "1.0";
let isRunning = false;
let isRefreshRunning = false;
logging_1.default.info(exports.NAME_SPACE, "Starting " + VERSION);
const on_init = async () => {
    logging_1.default.info(exports.NAME_SPACE, "On Init Start");
    await one_result_job();
    logging_1.default.info(exports.NAME_SPACE, "On Init End");
};
on_init();
async function one_result_job() {
    if (isRunning) {
        logging_1.default.info(exports.NAME_SPACE, "Still Running");
        return;
    }
    isRunning = true;
    try {
        const cur_date = new Date().toISOString().split("T")[0];
        // array kode
        const arrRpt = [
            { kode: "000", name: "from sp_rpt_052" },
            { kode: "001", name: "from sp_rpt_r_052i" },
            { kode: "002", name: "from sp_rpt_r_052ii" },
            { kode: "003", name: "from sp_rpt_r_052iii" },
            { kode: "004", name: "from sp_rpt_052iiii" },
            { kode: "005", name: "from sp_rpt_052iiiii" },
            { kode: "006", name: "from sp_rpt_052iiiiii" },
            { kode: "007", name: "from sp_rpt_052iiiiiii" },
            { kode: "008", name: "from sp_rpt_052iiiiiiii" },
            { kode: "009", name: "from sp_rpt_052iiiiiiiii" },
            // sekarang n
            { kode: "n006_unique", name: "from sp_rpt_r_052_Niiiiii" },
            { kode: "n007", name: "from sp_rpt_r_052_Niiiiiii" },
            { kode: "n008", name: "from sp_rpt_r_052_Niiiiiiii" },
            { kode: "n009", name: "from sp_rpt_r_052_Niiiiiiiii" },
            { kode: "online", name: "from sp_rpt_r_052_online" },
        ];
        const target = "0";
        logging_1.default.info(exports.NAME_SPACE, "Starting Job " + VERSION);
        if (arrRpt.length > 0) {
            for (let index = 0; index < arrRpt.length; index++) {
                const element = arrRpt[index];
                // rpt_052_000
                logging_1.default.info(exports.NAME_SPACE, "kode : " + element.kode);
                logging_1.default.info(exports.NAME_SPACE, "nama rpt : " + element.name);
                var resp = await (0, lib_052_master_1.rpt_052_000)(element.kode, cur_date, target);
                if (resp["status"] == "OK") {
                    logging_1.default.info(exports.NAME_SPACE, "\t Success");
                }
                else {
                    logging_1.default.error(exports.NAME_SPACE, "\t Error " + resp["message"]);
                }
                logging_1.default.info(exports.NAME_SPACE, "Wait " + config_1.default.delay + " second");
                await delay(config_1.default.delay * 1000);
            }
        }
        logging_1.default.info(exports.NAME_SPACE, "End Job");
        isRunning = false;
    }
    catch (e) {
        isRunning = false;
        if (e instanceof Error) {
            logging_1.default.error(exports.NAME_SPACE, e.message);
        }
        else {
            logging_1.default.error(exports.NAME_SPACE, "Unknown");
        }
    }
}
// console.log(config);
for (const sched of config_1.default.schedule) {
    Xcron.schedule(sched, async () => {
        await one_result_job();
    }, {
        timezone: "Asia/Jakarta",
    });
}
// ------------- function
async function delay(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
