-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.mjs
More file actions
2 lines (2 loc) · 23.5 KB
/
import.mjs
File metadata and controls
2 lines (2 loc) · 23.5 KB
1
2
// jsonstat-euro v3.0.0 Copyright 2026 Xavier Badosa https://jsonstat.com
var t="3.0.0";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},e(t)}var n="en";function l(t){var l=t.lang||n,i=t.version||"1.0",r=t.dataset||null,s=t.filter||null,o=t.label||null,a={class:"query",lang:l,version:i,dataset:r};return s&&"object"===e(s)&&0!==Object.keys(s).length&&(Object.keys(s).forEach(function(t){var e=s[t];Array.isArray(e)||(s[t]=[e]),0===s[t].length&&delete s[t]}),a.filter=s),o&&"object"===e(o)&&0!==Object.keys(o).length&&(a.label=o),a}function i(t,e){return Object.prototype.hasOwnProperty.call(t,e)}function r(t,e,n,r){var s,o=JSON.parse(JSON.stringify(t));if("string"==typeof r&&o.filter.hasOwnProperty("geo")&&-1!==o.filter.geo.indexOf(r)||(r=null),i(o,"filter")){switch(n){case"last":s=function(t){o.filter[t]=o.filter[t].slice(-1)};break;case"middle":s=function(t){var e=Math.round(o.filter[t].length/2);o.filter[t]=o.filter[t].slice(e-1,e)};break;default:s=function(t){o.filter[t]=o.filter[t].slice(0,1)}}Object.keys(o.filter).forEach(s),!0===e&&(delete o.filter.time,o.filter.lastTimePeriod=["1"]),r&&(o.filter.geo=[r])}return i(o,"label")&&i(o.label,"category")&&(Object.keys(o.label.category).forEach(function(t){o.label.category[t]=o.label.category[t].slice(0,1)}),!0===e&&delete o.label.category.time),l(o)}function s(t){var e=JSON.parse(JSON.stringify(t));return i(e,"filter")?(delete e.filter.time,e.filter.lastTimePeriod=["1"]):e.filter={lastTimePeriod:["1"]},l(e)}function o(t,e,n){void 0===n&&(n=Object.keys(e),e=l({dataset:null,filter:e}));var r=JSON.parse(JSON.stringify(t)),s=i(e,"filter"),o=i(e,"label")&&i(e.label,"category");return n.forEach(function(t){s&&i(e.filter,t)&&(i(r,"filter")||(r.filter={}),r.filter[t]=e.filter[t]),o&&i(e.label.category,t)&&(i(r,"label")?i(r.label,"category")||(r.label.category={}):r.label={},r.label.category[t]=e.label.category[t])}),l(r)}function a(t,e){var n=JSON.parse(JSON.stringify(t)),r=i(n,"filter"),s=i(n,"label"),o=s&&i(n.label,"category"),a=s&&i(n.label,"dimension");return e.forEach(function(t){r&&delete n.filter[t],s&&(o&&delete n.label.category[t],a&&delete n.label.dimension[t])}),l(n)}function u(t){return a(t,["time","lastTimePeriod","sinceTimePeriod"])}const c=t=>"[object ArrayBuffer]"===Object.prototype.toString.call(t.buffer),f=t=>["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array","BigInt64Array","BigUint64Array"].includes(t);function h(t,e){const n=(t,e,n)=>{if(0===Object.entries(t).length)return null;let l,i=[];if(n&&!f(n.name)&&(n=null),"string"==typeof t&&(t=[t]),Array.isArray(t)||c(t)){if(t.length===e)return n&&-1===t.findIndex(t=>null==t)?n.from(t):t;if(1===t.length)return Array(e).fill(t[0])}for(l=0;l<e;l++)i.push(void 0===t[l]?null:t[l]);return i},l=t=>{const e=void 0===t.index?t.label:t.index;return Array.isArray(e)?e.length:Object.keys(e).length};let i,r;if(this.length=0,this.id=[],null!=t)switch(this.class=t.class||"bundle",this.class){case"bundle":if(this.error=null,this.length=0,null===t||"object"!=typeof t)return void(this.class=null);if(t.hasOwnProperty("error"))return void(this.error=t.error);if("dataset"===t.class||"collection"===t.class||"dimension"===t.class)return new h(t);const s=Object.keys(t);this.__tree__=t,this.length=s.length,this.id=s;break;case"dataset":this.__tree__=i=t.hasOwnProperty("__tree__")?t.__tree__:t,this.label=i.label||null,this.note=i.note||null,this.link=i.link||null,this.href=i.href||null,this.updated=i.updated||null,this.source=i.source||null,this.extension=i.extension||null;let o=0;const a=i.size||i.dimension&&i.dimension.size;if(this.size=a,this.value=i.hasOwnProperty("value")&&null!==i.value&&0!==i.value.length?i.value:{},o=Array.isArray(this.value)||c(this.value)?this.value.length:a.reduce((t,e)=>t*e,1),this.value=n(this.value,o,e),this.status=i.hasOwnProperty("status")&&null!==i.status?n(i.status,o):null,i.hasOwnProperty("dimension")){const t=i.dimension,e=i.role||!i.version&&t.role||null,n=i.id||t.id,l=a.length,s=t=>{e.hasOwnProperty(t)||(e[t]=null)};if(!Array.isArray(n)||!Array.isArray(a)||n.length!=l)return;if(this.length=l,this.id=n,e&&(s("time"),s("geo"),s("metric"),s("classification")),e&&null===e.classification){let t=[];for(const n of["time","geo","metric"]){const l=e[n];null!==l&&(t=t.concat(l))}const l=n.filter(e=>!t.includes(e));e.classification=l.length?l:null}this.role=e,this.n=o;for(let e=0,l=this.length;e<l;e++)if(t[n[e]].category.hasOwnProperty("index")){if(Array.isArray(t[n[e]].category.index)){const l={};t[n[e]].category.index.forEach((t,e)=>{l[t]=e}),t[n[e]].category.index=l}}else{let l=0;for(r in t[n[e]].category.index={},t[n[e]].category.label)t[n[e]].category.index[r]=l++}}else this.length=0;break;case"dimension":if(!t.hasOwnProperty("__tree__"))return new h({version:"2.0",class:"dataset",dimension:{d:t},id:["d"],size:[l(t.category)],value:[null]}).Dimension(0);i=t.__tree__;const u=[],f=i.category;if(!i.hasOwnProperty("category"))return;if(!f.hasOwnProperty("label"))for(r in f.label={},f.index)f.label[r]=r;for(r in f.index)u[f.index[r]]=r;this.__tree__=i,this.label=i.label||null,this.note=i.note||null,this.link=i.link||null,this.href=i.href||null,this.id=u,this.length=u.length,this.role=t.role,this.hierarchy=f.hasOwnProperty("child"),this.extension=i.extension||null;break;case"category":const d=t.child;this.id=d,this.length=null===d?0:d.length,this.index=t.index,this.label=t.label,this.note=t.note||null,this.unit=t.unit,this.coordinates=t.coord;break;case"collection":if(this.length=0,this.label=t.label||null,this.note=t.note||null,this.link=t.link||null,this.href=t.href||null,this.updated=t.updated||null,this.source=t.source||null,this.extension=t.extension||null,null!==this.link&&t.link.item){const e=t.link.item;this.length=Array.isArray(e)?e.length:0,this.length&&(this.id=e.map(t=>t.href))}}}h.prototype.Item=function(t){return null!==this&&"collection"===this.class&&this.length?"number"==typeof t?t>this.length||t<0?null:this.link.item[t]:"object"!=typeof t||null===t?this.link.item:t.class?this.link.item.filter(e=>{if(e.class!==t.class)return!1;if("dataset"===t.class&&"boolean"==typeof t.embedded){const n=e.id&&e.size&&e.dimension;return t.embedded?n:!n}return!0}):null:null},h.prototype.Dataset=function(t){if(null===this)return null;if("dataset"===this.class)return void 0!==t?this:[this];if("collection"===this.class){const e=this.Item({class:"dataset",embedded:!0});if(void 0===t)return e.map(t=>new h(t));if("number"==typeof t&&t>=0&&t<e.length)return new h(e[t]);if("string"==typeof t){const n=e.find(e=>e.href===t);if(n)return new h(n)}return null}if("bundle"!==this.class)return null;if(void 0===t)return this.id.map(t=>this.Dataset(t));if("number"==typeof t){const e=this.id[t];return void 0!==e?this.Dataset(e):null}const e=this.__tree__[t];return void 0===e?null:new h({class:"dataset",__tree__:e})},h.prototype.Dimension=function(t,e){e="boolean"!=typeof e||e;const n=(t,e)=>{if(null!==t)for(let n in t)for(let l=null!==t[n]?t[n].length:0;l--;)if(t[n][l]===e)return n;return null};if(null===this||"dataset"!==this.class)return null;if(void 0===t)return this.id.map(t=>this.Dimension(t));if("number"==typeof t){const n=this.id[t];return void 0!==n?this.Dimension(n,e):null}const l=this.role;if("object"==typeof t){if(t.hasOwnProperty("role")){const i=[];for(let r=0,s=this.id.length;r<s;r++){const s=this.id[r];n(l,s)===t.role&&i.push(this.Dimension(s,e))}return void 0===i[0]?null:i}return null}const i=this.__tree__.dimension;if(void 0===i)return null;const r=i[t];return void 0===r?null:e?new h({class:"dimension",__tree__:r,role:n(l,t)}):((t,e)=>{let n=[];for(let l in t)n[t[l]]=e[l];return n})(r.category.index,r.category.label)},h.prototype.Category=function(t){if(null===this||"dimension"!==this.class)return null;if(void 0===t)return this.id.map(t=>this.Category(t));if("number"==typeof t){const e=this.id[t];return void 0!==e?this.Category(e):null}const e=this.__tree__.category;if(void 0===e)return null;const n=e.index[t];if(void 0===n)return null;const l=e.unit&&e.unit[t]||null,i=e.coordinates&&e.coordinates[t]||null,r=e.child&&e.child[t]||null,s=e.note&&e.note[t]||null;return new h({class:"category",index:n,label:e.label[t],note:s,child:r,unit:l,coord:i})},h.prototype.Dice=function(t,e,n){let l,i,r,s;const o=(t,e)=>t.hasOwnProperty(e)&&!!t[e];if(null===this||"dataset"!==this.class||null===this.value)return null;if("object"!=typeof t)return this;"object"!=typeof e?("boolean"==typeof e&&!0===e&&(l=!0),"boolean"==typeof n&&!0===n||(n=!1)):(l=o(e,"clone"),n=o(e,"drop"),i=o(e,"stringify"),r=o(e,"ovalue"),s=o(e,"ostatus"));let a,u=[],f=[];const d=this.value,y=l?new h(JSON.parse(JSON.stringify(this))):this,p=y.status,b=(t,e)=>{const n=((t,e)=>{const n={};return Array.isArray(t[e])?(t[e].forEach((t,e)=>{null!==t&&(n[String(e)]=t)}),n):t[e]})(t,e);delete t[e],t[e]=n};Array.isArray(t)&&(t=(t=>{const e={};return t.forEach(t=>{e[t[0]]=t[1]}),e})(t)),null===t&&(t={});const g=Object.keys(t);return g.length>0&&(g.forEach(e=>{const n=t[e];Array.isArray(n)||(t[e]=[n]),0===t[e].length&&delete t[e]}),n&&(t=(t=>{const e={};return Object.keys(t).forEach(n=>{e[n]=y.Dimension(n).id.filter(e=>-1===t[n].indexOf(e))}),e})(t)),y.Transform({type:"arrobj",content:"id",field:"id",status:!0}).forEach(e=>{let n=[];g.forEach(l=>{const i=t[l];let r=[];i.forEach(t=>{r.push(e[l]===t)}),n.push(-1!==r.indexOf(!0))}),-1===n.indexOf(!1)&&(u.push(e.value),f.push(e.status))}),g.forEach(e=>{const n=y.Dimension(e).id,l={};let i=0;y.size[y.id.indexOf(e)]=t[e].length,n.forEach(n=>{-1!==t[e].indexOf(n)&&(l[n]=i,i++)}),y.__tree__.dimension[e].category.index=l}),y.n=u.length,y.value=y.__tree__.value=c(d)?((t,e)=>e.from(t))(u,d.constructor):u,y.status=y.__tree__.status=null!==p?f:null),i?(a=y.__tree__,a.hasOwnProperty("id")||(a.version="2.0",a.hasOwnProperty("class")||(a.class="dataset"),a.id=a.dimension.id,a.size=a.dimension.size,delete a.dimension.id,delete a.dimension.size,a.dimension.hasOwnProperty("role")&&(a.role=a.dimension.role,delete a.dimension.role)),a.hasOwnProperty("status")&&-1!==["null","{}","[]"].indexOf(JSON.stringify(a.status))&&delete a.status,a.hasOwnProperty("role")&&(delete a.role.classification,["geo","time","metric"].forEach(t=>{null===a.role[t]&&delete a.role[t]})),r&&b(a,"value"),s&&a.hasOwnProperty("status")&&b(a,"status"),JSON.stringify(a)):y},h.prototype.Slice=function(t){return null===this||"dataset"!==this.class||0===Object.entries(this.value).length?null:void 0===t?this:(Array.isArray(t)||(t=Object.keys(t).map(e=>[e,t[e]])),this.Dice(t.map(t=>[t[0],[t[1]]])))},h.prototype.Data=function(t,e){let n,l,i=[],r=t=>{for(let e in t)if(t.hasOwnProperty(e))return e};if(null===this||"dataset"!==this.class||null===this.value)return null;if(void 0===t)return this.value.map((t,e)=>this.Data(e));if("boolean"!=typeof e&&(e=!0),"number"==typeof t){const n=this.value[t];return void 0===n?null:e?{value:n,status:this.status?this.status[t]:null}:n}let s="object";const o=this.__tree__,a=o.size||o.dimension&&o.dimension.size,u=a.length;if(Array.isArray(t)){if(!Array.isArray(t[0])){if(this.length!==t.length)return null;let l=1,r=0,s=[],o=[];for(n=0;n<u;n++)if(void 0!==t[n]){if("number"!=typeof t[n]||t[n]>=a[n])return null;l*=n>0?a[u-n]:1,r+=l*t[u-n-1]}else s.push(n),o.push(a[n]);if(s.length>1)return null;if(1===s.length){for(let l=0,r=o[0];l<r;l++){let r=[];for(n=0;n<u;n++)n!==s[0]?r.push(t[n]):r.push(l);i.push(this.Data(r,e))}return i}return e?{value:this.value[r],status:this.status?this.status[r]:null}:this.value[r]}s="array"}let c=[];const f=((t,e,n)=>{let l,i=[],s={};const o=t.dimension,a=t.id||o.id,u=t.size||o&&o.size;if("array"===n){for(l=e.length;l--;)s[e[l][0]]=e[l][1];e=s}for(let t=0,n=a.length;t<n;t++){const n=a[t],l=e[n];i.push("string"==typeof l?l:1===u[t]?r(o[n].category.index):null)}return i})(o,t,s),h=o.dimension,d=o.id||h.id;for(n=0,l=f.length;n<l;n++)c.push(h[d[n]].category.index[f[n]]);return this.Data(c,e)},h.prototype.toTable=function(t,e){if(null===this||"dataset"!==this.class||null===this.value)return null;1==arguments.length&&"function"==typeof t&&(e=t,t=null);const n=void 0!==(t=t||{field:"label",content:"label",vlabel:"Value",slabel:"Status",type:"array",status:!1,unit:!1,by:null,prefix:"",drop:[],meta:!1,comma:!1,bylabel:!1}).prefix?t.prefix:"";let l,i,r,s,o;"arrobj"!==t.type&&"objarr"!==t.type||void 0!==t.field||(t.field="id");const a="id"===t.field,u=t=>(a?"value":t)||"Value",c=t=>(a?"status":t)||"Status",h=this.__tree__;let d=!0===t.status;if("function"==typeof e){l=this.toTable(t);let n=[];const r="array"!==t.type?0:1,s="object"!==t.type?l.slice(r):l.rows.slice(0);for(o=s.length,i=0;i<o;i++){const t=e.call(this,s[i],i);void 0!==t&&n.push(t)}return"object"===t.type?{cols:l.cols,rows:n}:("array"===t.type&&n.unshift(l[0]),n)}if("arrobj"===t.type||"objarr"===t.type){let e=[],s=function(){},a={};const c=h.role&&h.role.metric,y=this,p=y.id,b=t.by&&-1!==p.indexOf(t.by)?t.by:null,g=!0===t.meta,m=void 0!==t.drop&&Array.isArray(t.drop)?t.drop:[],v=!0===t.comma,_=!0===t.bylabel,O=y.value.constructor,j=e=>{const l=u(t.vlabel);let i,r={};return"objarr"===t.type&&(i=null===b&&f(O.name)?t=>{r[t]=t===l?O.from(e,e=>e[t]):e.map(e=>e[t])}:t=>{r[t]=e.map(e=>e[t])},Object.keys(e[0]).forEach(i),e=r),g?(r={},p.forEach(t=>{const e=y.Dimension(t);r[t]={label:e.label,role:e.role,categories:{id:e.id,label:y.Dimension(t,!1)}}}),{meta:{label:y.label,source:y.source,updated:y.updated,id:p,status:d,unit:t.unit,by:b,bylabel:_,drop:null!==b&&m.length>0?m:null,prefix:null!==b?n||"":null,comma:v,dimensions:r},data:e}):e};b&&(t.field="id"),l=this.toTable({field:t.field,vlabel:t.vlabel,slabel:t.slabel,content:t.content,status:d});const x=l.shift();let w;if(null===b&&t.unit&&c){if("id"!==t.content)for(let t=c.length;t--;){const e=this.Dimension(c[t]);a[c[t]]={};for(let n=e.length;n--;)a[c[t]][e.Category(n).label]=e.id[n]}s=function(e,n){if(-1!==c.indexOf(e)){const l=h.dimension[e].category;l.unit?w.unit=l.unit["id"!==t.content?a[e][n]:n]:w.unit=null}},t.unit=!0}else t.unit=!1;for(o=l.length,i=0;i<o;i++){for(w={},r=l[i].length;r--;)w[x[r]]=l[i][r],s(x[r],l[i][r]);e.push(w)}if(v){const n=u(t.vlabel);e.forEach(function(t){null!==t[n]&&(t[n]=String(t[n]).replace(".",","))})}if(null!==b){const l={},i={};let r,s=[];m.forEach(function(t,e){(!y.Dimension(t)||y.Dimension(t).length>1)&&(m[e]="")});const o=p.filter(function(t){return t!==b&&-1===m.indexOf(t)}),a=y.Dimension(b),u=function(t,e){let n=[];return e.forEach(function(e){n.push(t[e])}),n.join("\t")},c=function(t,e){const n={};return e.forEach(function(e){n[e]=t[e]}),n};"id"!==t.content?_?r=function(t,e,l){t[e][`${n}${l[b]}`]=l.value}:(a.Category().forEach(function(t,e){i[t.label]=a.id[e]}),r=function(t,e,l){t[e][`${n}${i[l[b]]}`]=l.value}):r=function(t,e,l){t[e][`${n}${l[b]}`]=l.value},e.forEach(function(t){const e=u(t,o);void 0===l[e]&&(l[e]=c(t,o)),r(l,e,t,b)});for(let t in l)s.push(l[t]);return d=!1,j(s)}return j(e)}let y,p,b,g,m=[];if("object"===t.type){const t="number"==typeof this.value[0]||null===this.value[0]?"number":"string";y=function(t,e){const n=a&&t||e||t;S.push({id:t,label:n,type:"string"})},p=function(e,n,l){const i=u(e),r=c(n);l&&S.push({id:"status",label:r,type:"string"}),S.push({id:"value",label:i,type:t})},b=function(t){m.push({v:t})},g=function(t){m.push({v:t}),z.push({c:m})}}else y=function(t,e){const n=a&&t||e||t;S.push(n)},p=function(t,e,n){const l=u(t),i=c(e);n&&S.push(i),S.push(l),P.push(S)},b=function(t){m.push(t)},g=function(t){m.push(t),P.push(m)};const v=h.dimension,_=h.id||v.id,O=h.size||v.size,j=_.length;if(j!=O.length)return!1;let x=[],w=1,A=1,E=[],k=[],D=[],P=[],S=[],z=[];for(i=0;i<j;i++){const e=_[i];y(e,v[e].label),w*=O[i],A*=O[i];let n=[];for(r=0;r<O[i];r++)for(let e in v[_[i]].category.index)if(v[_[i]].category.index[e]===r){const l="id"!==t.content&&v[_[i]].category.label?v[_[i]].category.label[e]:e;n.push(l)}x.push(n),E.push(A)}for(p(t.vlabel,t.slabel,d),o=x.length,i=0;i<o;i++){let t=[];for(let e=0,n=x[i].length;e<n;e++)for(let n=0;n<w/E[i];n++)t.push(x[i][e]);k.push(t)}for(o=k.length,i=0;i<o;i++){let t=[],e=0;for(s=0;s<w;s++)t.push(k[i][e]),e++,e===k[i].length&&(e=0);D.push(t)}for(s=0;s<w;s++){m=[],o=k.length;for(let t=0;t<o;t++)b(D[t][s]);d&&b(this.status?this.status[s]:null),g(this.value[s])}return"object"===t.type?{cols:S,rows:z}:P},h.prototype.node=function(){return this.__tree__},h.prototype.toString=function(){return this.class},h.prototype.Unflatten=function(t){if(null===this||"dataset"!==this.class||null===this.value||"function"!=typeof t)return null;const e=this.id,n=this.size,l=e.length,i=[],r=[],s=new Array(l).fill(0);for(let t=0;t<l;t++)r.push(this.Dimension(t).id);for(let o=0;o<this.n;o++){const a={},u={value:this.value[o],status:this.status?this.status[o]:null};for(let t=0;t<l;t++)a[e[t]]=r[t][s[t]];const c=t(a,u,o,i);void 0!==c&&i.push(c);for(let t=l-1;t>=0&&(s[t]++,!(s[t]<n[t]));t--)s[t]=0}return i},h.prototype.Transform=function(t){if(null===this||"dataset"!==this.class||null===this.value)return null;if(null!=t){if("object"!=typeof t||void 0!==t.type&&"string"!=typeof t.type)return null}else t={};"arrobj"!==t.type&&"objarr"!==t.type||void 0!==t.field||(t.field="id");const e=this,n="arrobj"!==t.type&&"objarr"!==t.type?"array":t.type,l=e.id,i="array"!==n&&t.by&&l.includes(t.by)?t.by:null,r=t.comma||!1,s=null===i&&t.status||!1,o=t.content||"label",a=null===i?t.field||"label":"id",u="id"===t.field?"value":t.vlabel||"Value",c="id"===t.field?"status":t.slabel||"Status",f=t.meta||!1,h=i&&"string"==typeof t.prefix?t.prefix:"",d=Array.isArray(t.drop)?t.drop.filter(t=>{const n=e.Dimension(t);return n&&1===n.length}):[],y=l.filter(t=>!d.includes(t)),p=t=>null===t?null:String(t).replace(".",","),b=r?p:t=>t;let g,m,v;switch(n){case"array":{const t="label"===a?y.map(t=>e.Dimension(t).label):y,n=s?[c,u]:[u],l=y.map(t=>e.Dimension(t));m=t.concat(n);const i=s?(t,e)=>t.push(e.status):()=>{};g=(t,e)=>{const n=[];for(let e=0,i=y.length;e<i;e++){const i=y[e],r=l[e];n.push("label"===o?r.Category(t[i]).label:t[i])}return i(n,e),n.push(b(e.value)),n},v=e.Unflatten(g);break}case"objarr":{const t=r?t=>t.map(p):t=>t,n="id"===a?t=>t:t=>e.Dimension(t).label,l="id"===o?(t,e)=>e:(t,n)=>e.Dimension(t).Category(n).label,f={};if(i){const t=y.filter(t=>t!==i),r=e.Dimension(i),s=new Map,o={};t.forEach(t=>{f[n(t)]=[]}),r.id.forEach(t=>{const e=h+("id"===a?t:r.Category(t).label);o[t]=e,f[e]=[]});let u=0;g=function(e,r){const a=t.map(t=>e[t]).join("\0");let c;if(s.has(a))c=s.get(a);else{c=u++,s.set(a,c),t.forEach(t=>{f[n(t)].push(l(t,e[t]))});for(const e in f)!t.includes(e)&&f[e].length;Object.values(o).forEach(t=>{f[t].push(null)})}const h=o[e[i]],d=r.value;null!==d&&(f[h][c]=b(d))},e.Unflatten(g)}else s?(f[u]=t(e.value),f[c]=e.status):f[u]=t(e.value),y.forEach(t=>{f[n(t)]=[]}),g=function(t,e){y.forEach(e=>{f[n(e)].push(l(e,t[e]))})},e.Unflatten(g);v=f;break}default:{const t=new Map,n=[];y.forEach(t=>{const l=e.Dimension(t);n.push({id:t,dim:l,prop:"label"===a?l.label:t,isBy:i&&t===i})});const l=s?(t,e)=>{t[c]=e.status}:()=>{};g=(e,r)=>{const s={};let a;for(let t=0,l=n.length;t<l;t++){const l=n[t],i=l.id,r=l.prop,u=l.isBy,c=e[i],f="label"===o?l.dim.Category(c).label:c;u?a=f:s[r]=f}if(i){l(s,r);const e=Object.values(s).join("|");let n=t.get(e),i=!1;return n||(n=s,t.set(e,n),i=!0),n[h+a]=b(r.value),i?n:void 0}return s[u]=b(r.value),l(s,r),s},v=e.Unflatten(g)}}if(m&&v.unshift(m),f){const t={};return l.forEach(n=>{const l=e.Dimension(n);t[n]={label:l.label,role:l.role,categories:{id:l.id,label:e.Dimension(n,!1)}}}),{meta:{type:n,label:e.label,source:e.source,updated:e.updated,id:l,status:s,by:i,drop:d.length>0?d:null,prefix:null!==i?h||"":null,comma:r,dimensions:t},data:v}}return v};const d=t=>{if(!t.ok)throw new Error(`${t.status} ${t.statusText}`);return t.json()};function y(t,e,n){const l="object"==typeof e?e:{method:"GET"};return"function"!=typeof n&&(n=null),n||"function"!=typeof e||(n=e),"object"==typeof t?new h(t,n):"version"===t?"2.2.0":fetch?fetch(t,l).then(d).then(t=>new h(t,n)).catch(t=>{throw t}):void 0}function p(t){if("string"==typeof t){if(/https?:\/\//i.test(t))return t;t={dataset:t}}if("object"===e(t)&&null!==t&&!Array.isArray(t)&&t.dataset){t=l(t);var n="".concat("https://ec.europa.eu/eurostat/api/dissemination/statistics/").concat(t.version,"/data/").concat(t.dataset,"?lang=").concat(t.lang),i=[],r=t.filter||null;return r&&0!==Object.keys(r).length&&(Object.keys(r).forEach(function(t){r[t].forEach(function(e){i.push("".concat(t,"=").concat(e))})}),n+="&"+i.join("&")),n}return null}function b(t){t.role={geo:[],time:[],metric:[],classification:[]},t.id.forEach(function(e){switch(t.Dimension(e).role="time"===e||"geo"===e?e:"classification",e){case"geo":case"time":t.role[e].push(e);break;case"unit":case"s_adj":case"indic_co":case"na_item":case"indic":t.role.metric.push(e);break;default:t.role.classification.push(e)}}),t.role.geo.length||delete t.role.geo,t.role.time.length||delete t.role.time,t.role.metric.length||delete t.role.metric,t.role.classification.length||delete t.role.classification}function g(t){if(t)return y(p(t)).then(function(t){return"dataset"===t.class?(b(t),t):t},function(t){var e=t.message.slice(0,3),n=isNaN(e)?"418":e;return{class:"error",status:n,label:"418"!==n?t.message.slice(4):t.message}})}function m(t,e){"string"==typeof t&&(t={dataset:t});var l=!1!==e?s(t):t;return g(l).then(function(t){if("error"===t.class)return t;if(t.error)return{class:"error",status:t.error.status,label:t.error.label};var e={},i={},r={};return t.id.forEach(function(n){var l=t.Dimension(n);i[n]=l.label,e[n]=l.id,r[n]=l.Category().map(function(t){return t.label})}),{class:"query",dataset:l.dataset,filter:e,label:{dataset:t.label,dimension:i,category:r},lang:l.lang||n,version:l.version||"1.0"}})}function v(t,e,n,l){"string"==typeof t&&(t={dataset:t}),"boolean"!=typeof e&&(e=!1),"string"!=typeof n&&(n="first"),"string"!=typeof l&&(l=null);var s=i(t,"filter")?t.filter:null;if(s&&0!==Object.keys(s).length){var u=Object.keys(s);return m(o(t,s),!1).then(function(t){return"error"===t.class?t:m(a(r(t,e,n,l),u),!1).then(function(e){return"error"===e.class?e:o(t,e,u)})})}return m(t,!1).then(function(t){return t})}function _(t,e){function n(t){if(!t.ok)throw new Error(t.status+" "+t.statusText);return t.json()}if(t){if("string"==typeof t){if(/https?:\/\//i.test(t))return new Promise(function(t){t({class:"error",status:400,label:"A URL was passed to fetchEmptyDataset. Only dataset codes and queries are accepted. "})});t={dataset:t}}return t.filter={time_period:["null"]},fetch(p(t)).then(n).then(function(l){var i=y(l),r=l.id.indexOf("time"),s={};return i.id.forEach(function(t){"time"!==t&&(s[t]=[i.Dimension(t).id[0]])}),l.value=[],t.filter=s,fetch(p(t)).then(n).then(function(t){if(l.size[r]=t.size[r],l.dimension.time.category=t.dimension.time.category,e){var n=y(l);return"dataset"===n.class?(b(n),n):n}return l})},function(t){var e=t.message.slice(0,3),n=isNaN(e)?"418":e;return{class:"error",status:n,label:"418"!==n?t.message.slice(4):t.message}})}}function O(t,e){return t.extension.status.label[e]}function j(t){var e=i(t,"lang")?t.lang:n,l=i(t,"dataset")?t.dataset:null,r={lang:e.toUpperCase(),datasetId:l},s=Object.keys(t.filter),o=s.map(function(e){return t.filter[e].length}),a={};s.forEach(function(e){a[e]={label:t.label.dimension[e],category:{index:t.filter[e],label:{}}},t.filter[e].forEach(function(n,l){Object.defineProperty(a[e].category.label,n,{value:t.label.category[e][l]})})});var u=y({version:"2.0",class:"dataset",label:t.label.dataset,extension:r,id:s,size:o,dimension:a,value:[]});return b(u),u}export{o as addParamQuery,g as fetchDataset,_ as fetchEmptyDataset,v as fetchFullQuery,m as fetchQuery,j as getEmptyDataset,O as getStatusLabel,p as getURL,s as lastPeriodQuery,a as removeParamQuery,u as removeTimeQuery,b as setRole,r as simpleQuery,t as version};