"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const error_1=require("@b/utils/error"),db_1=require("@b/db");exports.metadata={summary:"Updates an API key",description:"Updates an API key's details such as name, type, permissions, IP whitelist, or IP restriction.",operationId:"updateApiKey",tags:["API Key Management"],logModule:"ADMIN_API",logTitle:"Update API",parameters:[{index:0,name:"id",in:"path",required:!0,description:"The ID of the API key to update",schema:{type:"string"}}],requestBody:{description:"Data for updating the API key",content:{"application/json":{schema:{type:"object",properties:{name:{type:"string",description:"Updated name of the API key"},type:{type:"string",enum:["plugin","user"],description:"Updated type of the API key (plugin or user)"},key:{type:"string",description:"Updated API key string"},permissions:{type:"array",items:{type:"string"},description:"Updated permissions associated with the API key"},ipWhitelist:{type:"array",items:{type:"string"},description:"Updated IP whitelist for the API key"},ipRestriction:{type:"boolean",description:"Updated IP restriction setting (true for restricted, false for unrestricted)"}}}}}},responses:{200:{description:"API key updated successfully",content:{"application/json":{schema:{type:"object",properties:{id:{type:"string"},name:{type:"string"},type:{type:"string"},key:{type:"string"},permissions:{type:"array",items:{type:"string"}},ipWhitelist:{type:"array",items:{type:"string"}},ipRestriction:{type:"boolean"}}}}}},401:{description:"Unauthorized"},404:{description:"API key not found"},500:{description:"Server error"}},requiresAuth:!0,permission:"edit.api.key"};exports.default=async e=>{const{params:t,body:i,ctx:s}=e,{id:r}=t,{userId:n,name:o,type:p,key:a,permissions:d,ipWhitelist:y,ipRestriction:c}=i;null==s||s.step("Validating API key");const u=await db_1.models.apiKey.findOne({where:{id:r}});if(!u)throw(0,error_1.createError)({statusCode:404,message:"API Key not found"});const l=e=>{try{return"string"==typeof e?JSON.parse(e):e}catch(t){return e}},m={};void 0!==d&&(m.permissions=sanitizePermissions(l(d)));void 0!==y&&(m.ipWhitelist=l(y));void 0!==n&&(m.userId=n);void 0!==o&&(m.name=o);void 0!==p&&(m.type=p);void 0!==a&&(m.key=a);void 0!==c&&(m.ipRestriction=Boolean(c));null==s||s.step("Updating API key");const I=await u.update(m);null==s||s.success();return I};const sanitizePermissions=e=>Array.isArray(e)?e.filter(e=>!["[","]"].includes(e)):[];