'use strict';
import _0x212a85 from 'ioredis';
import { logError } from '../logging/logger.js';
import { getBooleanEnvSetting, getStringEnvSetting } from '../../infra/env-functions.js';
import { toolboxEnvSettingKeys } from '../../infra/toolbox-env-setting-keys.js';
import { hasProperty, getPropertyValue } from '../../objects/property-value.js';
import { isObject } from '../../types/ensure-type.js';
import { formatCacheValue, parseCacheValue } from './fd-memory-cache.js';
import { parseJson } from '../../lib/utils.js';
import { htmlElementAttributes } from '../../constants/html-element-attributes.js';

var __defProp=Object['defineProperty'],__name=(_0x308c7,_0xfcf665)=>__defProp(_0x308c7,'name',{'value':_0xfcf665,'configurable':!![]});const redis=redisInit();async function redisQuit(){try{await redis?.['quit']();}catch(_0xfb816){logError('Error\x20quitting\x20Redis\x20connection',_0xfb816);}}__name(redisQuit,'redisQuit');async function setRemoteCache(_0x1504ae,_0x39137c,_0x415bfe){try{let _0x41df0c;if(_0x415bfe?.['absoluteExpirationRelativeToNowSeconds']!==void(0x1007+0xa94+0x1a9b*-0x1))_0x41df0c=_0x415bfe['absoluteExpirationRelativeToNowSeconds'];else _0x415bfe?.['slidingExpirationSeconds']!==void(0x277*0x3+-0x1153*0x1+0x9ee*0x1)&&(_0x41df0c=_0x415bfe['slidingExpirationSeconds']);const _0x12cde6=formatCacheValue(_0x39137c);_0x41df0c?await redis['set'](_0x1504ae,_0x12cde6,'EX',_0x41df0c):await redis['set'](_0x1504ae,_0x12cde6);}catch(_0x468429){logError('Error\x20setting\x20remote\x20cache\x20for\x20key:\x20'+_0x1504ae,_0x468429);}}__name(setRemoteCache,'setRemoteCache');async function tryGetFromRemoteCache(_0x36f3f7){try{const _0x139833=await redis['get'](_0x36f3f7);if(!_0x139833)return void(0x1d87+0x1576+-0x32fd);let _0x552d22;try{_0x552d22=parseJson(_0x139833);}catch{_0x552d22=parseCacheValue(_0x139833);}if(isObject(_0x552d22)&&hasProperty(_0x552d22,htmlElementAttributes['value']))return getPropertyValue(_0x552d22,htmlElementAttributes['value']);return _0x552d22;}catch(_0x4dfd31){return logError('Error\x20getting\x20remote\x20cache\x20for\x20key:\x20'+_0x36f3f7,_0x4dfd31),void(-0x1e24+-0x329*-0xb+-0x49f);}}__name(tryGetFromRemoteCache,'tryGetFromRemoteCache');async function removeFromRemoteCache(_0x3f1941){try{await redis['del'](_0x3f1941);}catch(_0xb3ec2c){logError('Error\x20removing\x20remote\x20cache\x20for\x20key:\x20'+_0x3f1941,_0xb3ec2c);}}__name(removeFromRemoteCache,'removeFromRemoteCache');async function remoteCacheHasKey(_0x25716e){try{return await redis['exists'](_0x25716e)===-0x4*0x195+-0x9c*0x1a+0x32b*0x7;}catch(_0x5341ce){return logError('Error\x20checking\x20existence\x20of\x20remote\x20cache\x20key:\x20'+_0x25716e,_0x5341ce),![];}}__name(remoteCacheHasKey,'remoteCacheHasKey');async function clearPermissionRemoteCache(_0x32b260){try{const _0x398164=await redis['keys']('*'),_0x455abb=_0x398164?.['filter'](_0x434f6c=>_0x32b260['some'](_0x120d0b=>_0x434f6c['startsWith'](_0x120d0b)));_0x455abb?.['length']&&await redis['del'](..._0x455abb);}catch(_0x164bb1){logError('Error\x20clearing\x20permission\x20remote\x20cache',_0x164bb1);}}__name(clearPermissionRemoteCache,'clearPermissionRemoteCache');async function flushRemoteCache(){try{redis&&await redis['flushall']();}catch(_0x24ad0c){logError('Error\x20flushing\x20remote\x20cache',_0x24ad0c);}}__name(flushRemoteCache,'flushRemoteCache');function redisInit(){try{if(getBooleanEnvSetting(toolboxEnvSettingKeys['useRemoteCache'])){const _0x4d5e12=getStringEnvSetting(toolboxEnvSettingKeys['fdRemoteCacheUrl']);if(_0x4d5e12)return new _0x212a85(_0x4d5e12,{'enableReadyCheck':![],'maxRetriesPerRequest':0x1,'lazyConnect':!![]});return new _0x212a85();}return void(0x9cf+0xc9*0x3+-0xc2a);}catch(_0x27fcec){return logError('Failed\x20to\x20initialize\x20Redis',_0x27fcec),void(0x214e+-0x13e0+0x12*-0xbf);}}__name(redisInit,'redisInit');

export { clearPermissionRemoteCache, flushRemoteCache, redisQuit, remoteCacheHasKey, removeFromRemoteCache, setRemoteCache, tryGetFromRemoteCache };
