"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.useCreateApiHandler=exports._parse=void 0;var a=require("react"),b=require("../utils/APITypes"),c=h(require("./../utils/api-factory.js")),d=h(require("@asyncapi/parser")),e=h(require("@asyncapi/avro-schema-parser")),f=h(require("@asyncapi/openapi-schema-parser")),g=require("lodash");function h(a){return a&&a.__esModule?a:{default:a}}function i(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){return void c(a)}h.done?b(i):Promise.resolve(i).then(d,e)}function j(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){i(h,d,e,f,g,"next",a)}function g(a){i(h,d,e,f,g,"throw",a)}var h=a.apply(b,c);f(void 0)})}}function k(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function l(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?k(Object(b),!0).forEach(function(c){m(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):k(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function m(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function n(a,b){return q(a)||p(a,b)||t(a,b)||o()}function o(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function p(a,b){var c=null==a?null:"undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(null!=c){var d,e,f=[],g=!0,h=!1;try{for(c=c.call(a);!(g=(d=c.next()).done)&&(f.push(d.value),!(b&&f.length===b));g=!0);}catch(a){h=!0,e=a}finally{try{g||null==c["return"]||c["return"]()}finally{if(h)throw e}}return f}}function q(a){if(Array.isArray(a))return a}function r(a){return v(a)||u(a)||t(a)||s()}function s(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function t(a,b){if(a){if("string"==typeof a)return w(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?w(a,b):void 0}}function u(a){if("undefined"!=typeof Symbol&&null!=a[Symbol.iterator]||null!=a["@@iterator"])return Array.from(a)}function v(a){if(Array.isArray(a))return w(a)}function w(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}d["default"].registerSchemaParser(e["default"]),d["default"].registerSchemaParser(f["default"]);var x=function(a){return d["default"].parse(a)};exports._parse=x;var y=function(a){var b=a.title,c=void 0===b?"":b,d=a.validationErrors,e=void 0===d?[]:d,f=a.detail,g=void 0===f?"":f,h=e.map(function(a){var b=a.title;return b}),i=[c,g].concat(r(h));return i},z=function(d){var e=(0,a.useState)({originalApi:d,apiHandler:null,loading:!0,error:null}),f=n(e,2),h=f[0],i=h.originalApi,k=h.apiHandler,m=h.loading,o=h.error,p=f[1];return(0,a.useEffect)(function(){p(function(a){return l(l({},a),{},{loading:!0})});var a=!1;return(0,b.getAPIType)(d)===b.APITypes.asyncapi?j(regeneratorRuntime.mark(function b(){var e,f,h;return regeneratorRuntime.wrap(function(b){for(;;)switch(b.prev=b.next){case 0:return b.prev=0,e=(0,g.cloneDeep)(d),b.next=4,x(d);case 4:if(f=b.sent,!a){b.next=7;break}return b.abrupt("return");case 7:p(function(a){return l(l({},a),{},{originalApi:e,apiHandler:(0,c["default"])(f),error:null,loading:!1})}),b.next=17;break;case 10:if(b.prev=10,b.t0=b["catch"](0),!a){b.next=14;break}return b.abrupt("return");case 14:console.error("Errors detected while parsing:",b.t0),h=y(b.t0).join("\n"),p(function(a){return l(l({},a),{},{error:h,loading:!1})});case 17:case"end":return b.stop();}},b,null,[[0,10]])}))():p(function(a){return l(l({},a),{},{originalApi:d,apiHandler:(0,c["default"])(d),error:null,loading:!1})}),function(){a=!0}},[d]),{originalApi:i,apiHandler:k,loading:m,error:o}};exports.useCreateApiHandler=z;