"use strict";var t=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.columns=void 0;const o=require("./Util"),i=t(require("../../log"));exports.columns=new class{constructor(){this.DEFAULT_OPTION={newline:"\n",padding:2,sort:!0,windowWidth:0},this.options=this.DEFAULT_OPTION}format(t,i){this.options=Object.assign(Object.assign({},this.DEFAULT_OPTION),i),this.options.sort&&(t=t.sort(this.strCompare));const s=this.options.windowWidth||process.stdout.columns,r=Math.max.apply(null,t.map(o.util.strWidthWithoutColors))+this.options.padding,n=Math.floor(s/r)||1,e=Math.ceil(t.length/n)||1;if(1===e)return t.map((t=>this.fillBlank(t,this.options.padding))).join("");if(1===n)return t.join(this.options.newline);const l=this.convertArr(e,n,t),h=this.calcColumnsMaxWidthArr(e,n,t.length,l);return l.map((t=>t.map(((t,i)=>{const s=o.util.strWidthWithoutColors(t);return this.fillBlank(t,h[i]-s)})).join(""))).join(this.options.newline)}strCompare(t,i){const s=o.util.removeColors(t),r=o.util.removeColors(i);return s.localeCompare(r)}fillBlank(t,i){const s=o.util.genEmptyList(i);return t.concat(s.join(""))}convertArr(t,o,i){const s=[];for(let r=0;r<t;r++){const t=[];for(let s=0;s<o;s++)r*o+s<i.length&&t.push(i[r*o+s]);s.push(t)}return s}calcColumnsMaxWidthArr(t,o,i,s){const r=[];for(let n=0;n<o;n++){const e=[];for(let r=0;r<t;r++)r*o+n<i&&e.push(s[r][n]);const l=this.maxWidthOfArray(e)+this.options.padding;r.push(l)}return r}maxWidthOfArray(t){let s=0;for(const r of t){if(!r||!r.trim()){i.default.warn("maxWidthOfArray","found empty item while calc maxWidthOfArray, please check!");continue}const t=o.util.strWidthWithoutColors(r.trim());s=t>s?t:s}return s}};