"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.updateOperationWithRequestValues=updateOperationWithRequestValues,exports.updateParametersWithUserValues=updateParametersWithUserValues,exports.updateRequestBodyWithUserValues=updateRequestBodyWithUserValues,exports.updateSecurityWithUserValues=updateSecurityWithUserValues,exports.updateOperationWithServerUserValues=updateOperationWithServerUserValues,exports.updateEnvVariablesWithUserValues=updateEnvVariablesWithUserValues,exports.isDirectRequestValues=isDirectRequestValues,exports.updateObjectProperties=updateObjectProperties,exports.createSecurityUpdates=createSecurityUpdates;const theme_1=require("@redocly/theme");function updateOperationWithRequestValues(e,t){if(0!==Object.keys(t).length)if(isDirectRequestValues(t))updateRequestBodyWithUserValues(e,t),updateSecurityWithUserValues(e,t.security),updateEnvVariablesWithUserValues(e,t.envVariables);else{updateOperationWithServerUserValues(e,t)}}function updateParametersWithUserValues(e,t){var s;if(!t)return;const{in:r,name:a}=e;if(isDirectRequestValues(t)){const u=t,i={header:u.headers,query:u.query,path:u.path,cookie:u.cookie};r&&(null===(s=i[r])||void 0===s?void 0:s[a])&&(e.example=i[r][a])}else Object.entries(t).forEach((([t,s])=>{var u;const i=s;if(!i)return;const l={header:i.headers,query:i.query,path:i.path,cookie:i.cookie};r&&(null===(u=l[r])||void 0===u?void 0:u[a])&&(e.serverValues||(e.serverValues={}),e.serverValues[t]={example:l[r][a]})}))}function updateRequestBodyWithUserValues(e,t,s){if(!e.requestBody||!(null==t?void 0:t.body))return;const r=e.requestBody.content;if(!(null==r?void 0:r.mediaTypes))return;const a=(e,r)=>{e[r]&&Object.keys(e[r]).forEach((a=>{var u,i;const l=null===(i=null===(u=e[r])||void 0===u?void 0:u[a])||void 0===i?void 0:i.value;let n;if(n=(0,theme_1.isObject)(l)?updateObjectProperties(l,t.body):t.body,n)if(s){const t=e[r][a];t.serverValues||(t.serverValues={}),t.serverValues[s]={value:n,rawValue:n},e[r][a]=Object.assign(Object.assign({},t),{mime:e.name})}else e[r][a]={value:n,rawValue:n,mime:e.name}}))};for(const e of r.mediaTypes)a(e,"examples"),a(e,"formExamples");return e}function updateSecurityWithUserValues(e,t,s){t&&e.security.length&&e.security.forEach((e=>{s?e.schemes.forEach((e=>{e.serverValues||(e.serverValues={});const r=createSecurityUpdates(e,t);e.serverValues[s]=r})):e.schemes.forEach((e=>{const s=createSecurityUpdates(e,t);Object.assign(e,s)}))}))}function updateOperationWithServerUserValues(e,t){e.servers.forEach((s=>{if(t[s.url]){const r=t[s.url];r&&(updateRequestBodyWithUserValues(e,r,s.url),updateSecurityWithUserValues(e,r.security,s.url),updateEnvVariablesWithUserValues(e,r.envVariables,s.url))}}))}function updateEnvVariablesWithUserValues(e,t,s){t&&0!==Object.keys(t).length&&(e.envVariables||(e.envVariables={}),s?(e.envVariables.serverValues||(e.envVariables.serverValues={}),e.envVariables.serverValues[s]=Object.assign(Object.assign({},e.envVariables.serverValues[s]),t)):e.envVariables.values=Object.assign(Object.assign({},e.envVariables.values),t))}function isDirectRequestValues(e){return!(!e||"object"!=typeof e)&&("headers"in e||"body"in e||"query"in e||"path"in e||"cookie"in e||"security"in e||"envVariables"in e)}function updateObjectProperties(e,t){if(!e||"object"!=typeof e)return t;if(Array.isArray(e)&&Array.isArray(t)){if(e.length>0&&"object"==typeof e[0]&&null!==e[0]){const s=Object.keys(e[0]),r=t.filter((e=>"object"==typeof e&&null!==e&&(s.some((t=>t in e))&&Object.keys(e).every((e=>s.includes(e))))));return r.length>0?r:e}return t}const s=Array.isArray(e)?[...e]:Object.assign({},e);for(const[r,a]of Object.entries(t))r in e&&(Array.isArray(e[r])||"object"==typeof a&&null!==a&&"object"==typeof e[r]?s[r]=updateObjectProperties(e[r],a):s[r]=a);return s}function createSecurityUpdates(e,t){var s,r,a;const u={"x-defaultAccessToken":null===(s=t.token)||void 0===s?void 0:s.access_token,"x-defaultTokenType":(null===(r=t.token)||void 0===r?void 0:r.token_type)||"Bearer","x-defaultClientId":t.client_id,"x-defaultClientSecret":t.client_secret,scopes:t.scopes||e.scopes};return"http"===e.type&&"basic"===e.scheme?(u["x-defaultUsername"]=t.username,u["x-defaultPassword"]=t.password,delete u["x-defaultAccessToken"],delete u["x-defaultTokenType"]):"oauth2"!==e.type||(null===(a=t.token)||void 0===a?void 0:a.access_token)||(delete u["x-defaultAccessToken"],delete u["x-defaultTokenType"]),u}
//# sourceMappingURL=configure-helpers.js.map