"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.useAsyncAPIExamplePayload=exports.useAsyncAPIExampleHeader=void 0;var a=require("react"),b=f(require("@ovotech/avro-mock-generator")),c=f(require("../utils/generator")),d=require("../contexts/ApiHandler"),e=require("react-i18next");function f(a){return a&&a.__esModule?a:{default:a}}function g(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 h(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){g(i,d,e,f,h,"next",a)}function h(a){g(i,d,e,f,h,"throw",a)}var i=a.apply(b,c);f(void 0)})}}function i(a,b){return n(a)||m(a,b)||k(a,b)||j()}function j(){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 k(a,b){if(a){if("string"==typeof a)return l(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)?l(a,b):void 0}}function l(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}function m(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 n(a){if(Array.isArray(a))return a}var o=function(f){var g=(0,d.useApi)(),j=g.apiHandler,k=(0,a.useState)({loading:!0,example:null,error:null}),l=i(k,2),m=l[0],n=l[1],o=(0,a.useMemo)(function(){return j.getFirstSchema(f)},[j,f]),p=o.example,q=o.payload,r=o.type,s=(0,e.useTranslation)(),u=s.t;return(0,a.useEffect)(function(){var a=function(){var a=h(regeneratorRuntime.mark(function a(d,e,f){var g,h,i,k,l;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(g=null,h=null,i=j.isPrimitiveSchema(e),!d){a.next=7;break}n({loading:!1,example:d,error:h}),a.next=28;break;case 7:if(!e){a.next=27;break}if(a.prev=8,!j.isAvroSchema(f)||i){a.next=13;break}g=(0,b["default"])(e),a.next=21;break;case 13:if(!(j.isAsyncApiSchema(f)||j.isOpenApiSchema(f)||j.isJSONSchema(f)||i)){a.next=20;break}return a.next=16,c["default"].generateAsyncApiResponse({api:j.api,schema:e,contentType:"application/json",highlight:!1});case 16:k=a.sent,g=JSON.parse(k.example),a.next=21;break;case 20:g=u("unsupported_schema_format",{type:f});case 21:a.next=27;break;case 23:a.prev=23,a.t0=a["catch"](8),l=a.t0.message?a.t0.message:a.t0,h=u("example_generator_error",{error:l});case 27:n({loading:!1,example:g,error:h});case 28:case"end":return a.stop();}},a,null,[[8,23]])}));return function(){return a.apply(this,arguments)}}();a(p,q,r)},[p,q,r,j,u]),m};exports.useAsyncAPIExamplePayload=o;var p=function(b){var f=(0,d.useApi)(),g=f.apiHandler,j=(0,a.useState)({loading:!0,example:null,error:null}),k=i(j,2),l=k[0],m=k[1],n=(0,a.useMemo)(function(){return g.getFirstMessageHeader(b)},[g,b]),o=n.schema,p=n.example,q=(0,e.useTranslation)(),r=q.t;return(0,a.useEffect)(function(){var a=function(){var a=h(regeneratorRuntime.mark(function a(b,d){var e,f,h,i;return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(e=null,f=null,!b){a.next=6;break}m({loading:!1,example:b,error:f}),a.next=19;break;case 6:if(!d){a.next=18;break}return a.prev=7,a.next=10,c["default"].generateAsyncApiResponse({api:g.api,schema:d,contentType:"application/json",highlight:!1});case 10:h=a.sent,e=JSON.parse(h.example),a.next=18;break;case 14:a.prev=14,a.t0=a["catch"](7),i=a.t0.message?a.t0.message:a.t0,f=r("example_generator_error",{error:i});case 18:m({loading:!1,example:e,error:f});case 19:case"end":return a.stop();}},a,null,[[7,14]])}));return function(){return a.apply(this,arguments)}}();a(p,o)},[p,o,g,r]),l};exports.useAsyncAPIExampleHeader=p;