import{createClient}from"redis";const defaults={host:"localhost",port:"6379",user:null,pass:null},VERSION=1,fields=["map","ctx","reg","tag","doc","cfg"];import StorageInterface from"../interface.js";import{toArray}from"../../common.js";function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_\-]/g,"")}let DB,TRX;export default function RedisDB(a,b={}){return this&&this.constructor===RedisDB?void("object"==typeof a&&(b=a,a=a.name),!a&&console.info("Default storage space was used, because a name was not passed."),this.id=(a?sanitize(a):"flexsearch")+"|",this.field=b.field?"-"+sanitize(b.field):"",this.type=b.type||"",this.fastupdate=!0,this.db=b.db||DB||null,this.support_tag_search=!0,this.resolution=9,this.resolution_ctx=9,Object.assign(defaults,b),this.db&&delete defaults.db):new RedisDB(a,b)}RedisDB.prototype.mount=function(a){return a.index?a.mount(this):(a.db=this,this.resolution=a.resolution,this.resolution_ctx=a.resolution_ctx,this.open())},RedisDB.prototype.open=async function(){if(this.db)return this.db;if(DB)return this.db=DB;let a=defaults.url;return a||(a=defaults.user?`redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}`:`redis://${defaults.host}:${defaults.port}`),this.db=DB=await createClient(a).on("error",a=>console.error(a)).connect()},RedisDB.prototype.close=async function(){return DB&&(await this.db.disconnect()),this.db=DB=null,this},RedisDB.prototype.destroy=function(){return this.clear()},RedisDB.prototype.clear=function(){return this.db.unlink([this.id+"map"+this.field,this.id+"ctx"+this.field,this.id+"tag"+this.field,this.id+"cfg"+this.field,this.id+"doc",this.id+"reg"])};function create_result(a,b,c,d,e){if(c){if("number"===b)for(let c,e,f=0;f=a.length)return[];if(!b&&!c)return a;const f=a.slice(c,c+b);return d?e.enrich(f):f})},RedisDB.prototype.enrich=function(a){return"object"!=typeof a&&(a=[a]),this.db.hmGet(this.id+"doc","number"===this.type?a.map(a=>""+a):a).then(function(b){for(let c=0;ce,k.push(c+(f?d:e)+":"+(f?e:d)),l.push(1),e=d}else{const a=this.id+"map"+this.field+":";for(let c=0;c