"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t=e(require("chalk")),n=require("mocha"),r=e(require("axios")),i=require("date-fns");const s=function(){function e(){}return e.prototype.then=function(t,n){const r=new e,i=this.s;if(i){const e=1&i?t:n;if(e){try{o(r,1,e(this.v))}catch(e){o(r,2,e)}return r}return this}return this.o=function(e){try{const i=e.v;1&e.s?o(r,1,t?t(i):i):n?o(r,1,n(i)):o(r,2,i)}catch(e){o(r,2,e)}},r},e}();function o(e,t,n){if(!e.s){if(n instanceof s){if(!n.s)return void(n.o=o.bind(null,e,t));1&t&&(t=n.s),n=n.v}if(n&&n.then)return void n.then(o.bind(null,e,t),o.bind(null,e,2));e.s=t,e.v=n;const r=e.o;r&&r(e)}}function a(e){return e instanceof s&&1&e.s}function u(e,t){try{var n=e()}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}var d;"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator"))),function(e){e[e.Passed=1]="Passed",e[e.Blocked=2]="Blocked",e[e.Untested=3]="Untested",e[e.Retest=4]="Retest",e[e.Failed=5]="Failed"}(d||(d={}));var c=function(e,t,n){this.id=e,this.name=t,this.description=n,this.caseIds=[],this.runId=null},l=function(){function e(e){this.options=e,this.suites=[],this.testResults=[],this.axiosInstance=r.create({baseURL:"https://"+e.domain+"/index.php?/api/v2",headers:{"Content-Type":"application/json"},auth:{username:e.username,password:e.password}}),this.projectId=e.projectId,this.planId=e.planId,this.runId=e.runId,this.today=i.format((new Date).getTime(),"yyyy/MM/dd")}var n,l=e.prototype;return l.addFailedTest=function(e,t){var n=this.runId?this.runId:this.findRunIdForCase(e);if(n){console.log("- Add Failed Test: ",t.title);var r=this.getElapsedFromTest(t);this.testResults.push({case_id:e,status_id:d.Failed,run_id:n,elapsed:r,comment:t.err.message})}else console.warn("Could not find a Run ID for failed Test Case: ",e)},l.addPassedTest=function(e,t){var n=this.runId?this.runId:this.findRunIdForCase(e);if(n){var r=this.getElapsedFromTest(t);this.testResults.push({case_id:e,status_id:d.Passed,run_id:n,elapsed:r,comment:"Execution time: "+t.duration+"ms"})}else console.warn("Could not find a Run ID for passed Test Case: ",e)},l.constructSuites=function(){try{var n=this,r=u((function(){return Promise.resolve(n.axiosInstance.get("/get_suites/"+n.projectId)).then((function(t){function r(){return Promise.resolve(n.getCases()).then((function(){}))}var i=t.data.filter((function(e){return!e.name.includes("Master")})).sort((function(e,t){return e.name>t.name?1:e.name<t.name?-1:0})),s=Array.isArray(i),o=0;for(i=s?i:i[Symbol.iterator]();;){var a;if(s){if(o>=i.length)break;a=i[o++]}else{if((o=i.next()).done)break;a=o.value}n.suites.push(new c(a.id,a.name,a.description))}var u=[],d=function(){if(n.planId)return Promise.resolve(n.axiosInstance.get("/get_plan/"+n.planId)).then((function(t){function r(){var e=function(){if(r){if(i>=t.length)return"break";s=t[i++]}else{if((i=t.next()).done)return"break";s=i.value}var e=s;n.suites.forEach((function(t){t.id===e.suite_id&&(t.runId=e.id)}))},t=u,r=Array.isArray(t),i=0;for(t=r?t:t[Symbol.iterator]();;){var s;if("break"===e())break}}var i=t.data;i.entries&&i.entries.length&&(u=e.flat(i.entries.map((function(e){return e.runs}))));var s=function(){if(!u.length)return Promise.resolve(n.createRuns()).then((function(e){u=e}))}();return s&&s.then?s.then(r):r()}))}();return d&&d.then?d.then(r):r()}))}),(function(e){console.log(t.redBright.underline.bold("Internal error",e))}));return Promise.resolve(r&&r.then?r.then((function(){})):void 0)}catch(e){return Promise.reject(e)}},l.publish=function(){try{var e=this,n=e.constructTestResult(),r=Object.entries(n).map((function(t){return e.axiosInstance.post("/add_results_for_cases/"+t[0],{results:t[1]})})),i=u((function(){return Promise.resolve(Promise.all(r)).then((function(){console.log("\n",t.magenta.underline.bold("(TestRail Reporter)")),console.log("\n"," - Results are published to "+t.magenta(e.planId?"https://"+e.options.domain+"/index.php?/plans/view/"+e.planId:"https://"+e.options.domain+"/index.php?/runs/view/"+e.runId),"\n")}))}),(function(e){console.error(e)}));return Promise.resolve(i&&i.then?i.then((function(){})):void 0)}catch(e){return Promise.reject(e)}},l.getElapsedFromTest=function(e){return e.duration?Math.ceil(e.duration/1e3)+"s":null},l.constructTestResult=function(){return this.testResults.reduce((function(e,t){var n=t.run_id,r=function(e,t){if(null==e)return{};var n,r,i={},s=Object.keys(e);for(r=0;r<s.length;r++)t.indexOf(n=s[r])>=0||(i[n]=e[n]);return i}(t,["run_id"]);return e[n]=e[n]?[].concat(e[n],[r]):[r],e}),{})},l.findRunIdForCase=function(e){var t=this.suites.find((function(t){return t.caseIds.includes(e)}));return t?t.runId:0},l.createRuns=function(){try{var e=this,t=[],n=0,r=function(e,t,n){for(var r;;){var i=e();if(a(i)&&(i=i.v),!i)return u;if(i.then){r=0;break}var u=n();if(u&&u.then){if(!a(u)){r=1;break}u=u.s}if(t){var d=t();if(d&&d.then&&!a(d)){r=2;break}}}var c=new s,l=o.bind(null,c,2);return(0===r?i.then(h):1===r?u.then(f):d.then(p)).then(void 0,l),c;function f(r){u=r;do{if(t&&(d=t())&&d.then&&!a(d))return void d.then(p).then(void 0,l);if(!(i=e())||a(i)&&!i.v)return void o(c,1,u);if(i.then)return void i.then(h).then(void 0,l);a(u=n())&&(u=u.v)}while(!u||!u.then);u.then(f).then(void 0,l)}function h(e){e?(u=n())&&u.then?u.then(f).then(void 0,l):f(u):o(c,1,u)}function p(){(i=e())?i.then?i.then(h).then(void 0,l):h(i):o(c,1,u)}}((function(){return n<e.suites.length}),(function(){return n++}),(function(){var r=e.suites[n];return Promise.resolve(e.axiosInstance.post("/add_plan_entry/"+e.planId,{suite_id:r.id,name:r.name,description:r.description+" "+e.today}).then((function(e){return e.data.runs[0]}))).then((function(e){t.push(e)}))}));return Promise.resolve(r&&r.then?r.then((function(){return t})):t)}catch(e){return Promise.reject(e)}},l.getCases=function(){try{var t=this,n=t.suites.map((function(e){return t.axiosInstance.get("/get_cases/"+t.projectId+"&suite_id="+e.id)}));return Promise.resolve(Promise.all(n)).then((function(n){var r=function(){if(s){if(o>=i.length)return"break";a=i[o++]}else{if((o=i.next()).done)return"break";a=o.value}var e=a;t.suites.forEach((function(t){t.id===e.suite_id&&t.caseIds.push(e.id)}))},i=e.flat(n.map((function(e){return e.data}))),s=Array.isArray(i),o=0;for(i=s?i:i[Symbol.iterator]();;){var a;if("break"===r())break}}))}catch(e){return Promise.reject(e)}},e.flat=function(e){return e.reduce((function(e,t){return e.concat(t)}),[])},(n=[{key:"results",get:function(){return this.testResults}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(e.prototype,n),e}(),f=function(e){for(var t,n=/\bT?C(\d+)\b/g,r=null;null!==(t=n.exec(e));)r=parseInt(t[1]);return r};module.exports=function(e){var n,r;function i(n,r){var s;(s=e.call(this,n,r)||this).handleTest=function(e){return function(t){var n=f(t.title);n&&("fail"===e?s.testRail.addFailedTest(n,t):s.testRail.addPassedTest(n,t))}},s.handleEnd=function(){if(!s.testRail.results.length)return console.log("\n",t.magenta.underline.bold("(TestRail Reporter)")),void console.warn("\n","No test cases were matched. Ensure that your tests are declared correctly and matches CXXXX","\n");s.testRail.publish()};var o=r.reporterOptions;if(i.validate(o),!o.planId&&!o.runId)throw new Error("CypressTestRailReporter requires either `planId` or `runId` to be configured.");if(!o.projectId)throw new Error("CypressTestRailReporter required `projectId` to be configured.");return s.testRail=new l(o),s.report(),s}return r=e,(n=i).prototype=Object.create(r.prototype),n.prototype.constructor=n,n.__proto__=r,i.prototype.report=function(){this.runner.once("start",this.testRail.constructSuites.bind(this.testRail)),this.runner.on("fail",this.handleTest("fail")),this.runner.on("pass",this.handleTest("pass")),this.runner.on("end",this.handleEnd)},i.validate=function(e){if(null==e)throw new Error("No reporterOptions");for(var t in e)if(!e[t])throw new Error("Unknown "+t+" value, please update your reporterOptions.")},i}(n.reporters.Base);
//# sourceMappingURL=cypress-testrail-reporter.cjs.production.min.js.map
