/**Copyright (c) 2009-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.**/
"use strict";const moment=require("moment"),fs=require("fs"),fsPromises=fs.promises,InternalServerError=require("../Errors/InternalServerError"),winston=require("winston"),logger=winston.createLogger({transports:[new winston.transports.Console({timestamp:!0})],exitOnError:!1});
/**
* Class containing utils.
* @memberof mdxWebApiCore.Utils
* */
class Utils{
/**
* Extracts place hierarchy from input place
* @public
* @static
* @param {string} place
* @returns {string}
* @example
* const mdx = require("@nvidia-mdx/web-api-core");
* // returns city/intersection
* let result = mdx.Utils.Utils.getPlaceHierarchy(city=abc/intersection=xyz);
*/
static getPlaceHierarchy(e){let t=e.split("/"),r=new Array;for(let e of t){let t=e.split("=");r.push(t[0])}return r.join("/")}
/**
* Compares two timestamps.
* @public
* @static
* @param {string} timestamp1
* @param {('>'|'>='|'<'|'<='|'==')} comparison
* @param {string} timestamp2
* @returns {boolean} Returns whether the comparison is true or false
* @example
* const mdx = require("@nvidia-mdx/web-api-core");
* let fromTimestamp = "2023-01-12T11:20:10.000Z";
* let toTimestamp = "2023-01-12T14:20:10.000Z";
* let result = mdx.Utils.Utils.tsCompare(fromTimestamp,"<",toTimestamp);
*/static tsCompare(e,t,r){switch(t){case">":return moment.utc(e).isAfter(moment.utc(r));case">=":return moment.utc(e).isSameOrAfter(moment.utc(r));case"<":return moment.utc(e).isBefore(moment.utc(r));case"<=":return moment.utc(e).isSameOrBefore(moment.utc(r));case"==":return moment.utc(e).isSame(moment.utc(r));default:throw new InternalServerError("Invalid comparison operator. Valid comparson operators include: '>', '>=', '<', '<=', '=='")}}
/**
* Calculates set difference.
* @public
* @static
* @param {Set} set1
* @param {Set} set2
* @returns {Set} Returns a set which is a set difference of the input sets
* @example
* const mdx = require("@nvidia-mdx/web-api-core");
* let result = mdx.Utils.Utils.setDifference(new Set([1,2,3]),new Set([2,3,4]));
*/static setDifference(e,t){return new Set([...e].filter((e=>!t.has(e))))}
/**
* Calculates set intersection.
* @public
* @static
* @param {Set} set1
* @param {Set} set2
* @returns {Set} Returns a set which is a set intersection of the input sets
* @example
* const mdx = require("@nvidia-mdx/web-api-core");
* let result = mdx.Utils.Utils.setIntersection(new Set([1,2,3]),new Set([2,3,4]));
*/static setIntersection(e,t){return new Set([...e].filter((e=>t.has(e))))}
/**
* Asynchronously waits for a specified amount of time.
* @public
* @static
* @async
* @param {number} ms - The number of milliseconds to wait. ms must be an integer.
* @returns {Promise<void>} A promise that resolves after the specified time has elapsed.
* @example
* const mdx = require("@nvidia-mdx/web-api-core");
* await mdx.Utils.Utils.sleep(2000);
*/static async sleep(e){await new Promise((t=>setTimeout(t,e)))}
/**
* Used to delete files
* @public
* @static
* @async
* @param {Array<string>} filePaths
* @returns {Promise<Object>} A success message is returned once files are deleted
* @example
* const mdx = require("@nvidia-mdx/web-api-core");
* let filePaths = ["/path/to/file1","/path/to/file2"];
* let result = await mdx.Utils.Utils.deleteFiles(filePaths);
*/static async deleteFiles(e){let t=new Array;for(let r of e)fs.existsSync(r)?t.push(fsPromises.unlink(r)):logger.warn(`[FILE] Cannot delete the file: '${r}' as it doesn't exist.`);return await Promise.all(t),{success:!0}}
/**
* Wraps a function to catch any errors thrown and handle them using a provided function.
* @public
* @static
* @param {Function} fn - The function to wrap.
* @returns {Function} A new function that calls the original function and catches any errors thrown.
* @example
* const express = require('express');
* let router = express.Router();
* router.route("/new-endpoint").get(mdx.Utils.Utils.expressAsyncWrapper(async(req,res,next)=>{}));
*/static expressAsyncWrapper=e=>(...t)=>e(...t).catch(t[2])}module.exports=Utils;