"use strict";
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 });
exports.disable = exports.confirm = exports.getAll = void 0;
const lodash_1 = __importDefault(require("lodash"));
const constraints_1 = __importDefault(require("../validation/constraints"));
const errorTypes_1 = __importDefault(require("../utils/errorTypes"));
const MonitoringService = __importStar(require("../services/monitoringService"));
const Resources = __importStar(require("../utils/resources"));
const strings_1 = require("../utils/strings");
const validator_1 = require("../validation/validator");
const isValidToken = validator_1.forConstraint(constraints_1.default.token, false);
async function doGetAll(req) {
    const restParams = await Resources.getValidRestParams('list', null, req);
    const { monitoringStates, total } = await MonitoringService.getAll(restParams);
    return {
        total,
        result: lodash_1.default.map(monitoringStates, function (state) {
            state.mapId = lodash_1.default.toLower(state.mac).replace(/:/g, '');
            return state;
        })
    };
}
function getAll(req, res) {
    doGetAll(req)
        .then(({ total, result }) => {
        res.set('X-Total-Count', total.toString(10));
        Resources.success(res, result);
    })
        .catch(err => Resources.error(res, err));
}
exports.getAll = getAll;
function confirm(req, res) {
    const data = Resources.getData(req);
    const token = strings_1.normalizeString(data.token);
    if (!isValidToken(token)) {
        return Resources.error(res, { data: 'Invalid token.', type: errorTypes_1.default.badRequest });
    }
    MonitoringService.confirm(token)
        .then(node => Resources.success(res, {
        hostname: node.hostname,
        mac: node.mac,
        email: node.email,
        monitoring: node.monitoring,
        monitoringConfirmed: node.monitoringConfirmed
    }))
        .catch(err => Resources.error(res, err));
}
exports.confirm = confirm;
function disable(req, res) {
    const data = Resources.getData(req);
    const token = strings_1.normalizeString(data.token);
    if (!isValidToken(token)) {
        return Resources.error(res, { data: 'Invalid token.', type: errorTypes_1.default.badRequest });
    }
    MonitoringService.disable(token)
        .then(node => Resources.success(res, {
        hostname: node.hostname,
        mac: node.mac,
        email: node.email,
        monitoring: node.monitoring
    }))
        .catch(err => Resources.error(res, err));
}
exports.disable = disable;
//# sourceMappingURL=monitoringResource.js.map