import config from"@arcgis/core/config";import{isArcGisApiVersion3x,isArcGisApiVersionAtLeast}from"./ArcGisApiVersion.js";import{validation}from"../../Validation.js";class RegisterCorsUrl{constructor(){this.esriConfig=config,this.isApi3x=isArcGisApiVersion3x,this.isApiAtLeast49=isArcGisApiVersionAtLeast("4.9")}execute(r){var i=validation.string("url",r.url,validation.never),r=validation.boolean("withCredentials",r.withCredentials,!1);return{success:this.registerCorsUrl(i,r)}}registerCorsUrl(r,i=!1){var s=this.isApi3x?this.esriConfig.defaults&&this.esriConfig.defaults.io:this.esriConfig.request;return!!s&&(this.isApiAtLeast49?this.tryAddTrustedServer(this.esriConfig.request.trustedServers,r,i):(this.configureCorsSupport(s,i),this.tryAddCorsEnabledServer(s.corsEnabledServers,r,i)))}configureCorsSupport(r,i){var s="with-credentials";(!r.useCors||i&&r.useCors!==s)&&(r.useCors=!i||s)}tryAddCorsEnabledServer(i,s,r=!1){if(!i||!s)return!1;r=r?{host:s,withCredentials:r}:s;let e=i.indexOf(s);if(-1===e)for(let r=0;r<i.length;r++){var t=i[r];if("object"==typeof t&&t.host===s){e=r;break}}return-1===e?i.push(r):i[e]=r,!0}tryAddTrustedServer(r,i,s){if(!i)return!1;if(s){if(!r)return!1;-1===r.indexOf(i)&&r.push(i)}return!0}}RegisterCorsUrl.action="gcx:wf:arcgis::RegisterCorsUrl",RegisterCorsUrl.suite="gcx:wf:builtin";export{RegisterCorsUrl};