"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"));
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 {
        let dateTime = new Date();
        let dateTimeString = dateTime.toString();
        logging_1.default.info(exports.NAME_SPACE, "Starting Job " + VERSION);
        logging_1.default.info(exports.NAME_SPACE, "dateTimeString " + dateTimeString);
        // rpt_052_000
        // logging.info(NAME_SPACE, "rpt_052_000");
        // var resp = await rpt_052_000("000",dateTimeString,"0");
        // if (resp["status"] == "OK") {
        //   logging.info(NAME_SPACE, "\t Success");
        // } else {
        //   logging.error(NAME_SPACE, "\t Error " + resp["message"]);
        // }
        // logging.info(NAME_SPACE, "Wait " + config.delay + " second");
        // await delay(config.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));
}
