import{wrapAnsi}from"../../../../../fast-wrap-ansi@0.2.2/node_modules/fast-wrap-ansi/lib/main.js";import{require_src}from"../../../../../sisteransi@1.0.5/node_modules/sisteransi/src/index.js";import{styleText}from"node:util";import"node:readline";import E from"node:readline";import{stdin,stdout}from"node:process";import"node:tty";var import_src=require_src();function f(e,t,n){if(!n.some(e=>!e.disabled))return e;let r=e+t,i=Math.max(n.length-1,0),a=r<0?i:r>i?0:r;return n[a].disabled?f(a,t<0?-1:1,n):a}const h={actions:new Set([`up`,`down`,`left`,`right`,`space`,`enter`,`cancel`]),aliases:new Map([[`k`,`up`],[`j`,`down`],[`h`,`left`],[`l`,`right`],[``,`cancel`],[`escape`,`cancel`]]),messages:{cancel:`Canceled`,error:`Something went wrong`},withGuide:!0,date:{monthNames:[`January`,`February`,`March`,`April`,`May`,`June`,`July`,`August`,`September`,`October`,`November`,`December`],messages:{required:`Please enter a valid date`,invalidMonth:`There are only 12 months in a year`,invalidDay:(e,t)=>`There are only ${e} days in ${t}`,afterMin:e=>`Date must be on or after ${e.toISOString().slice(0,10)}`,beforeMax:e=>`Date must be on or before ${e.toISOString().slice(0,10)}`}}};function C(e,t){if(typeof e==`string`)return h.aliases.get(e)===t;for(let n of e)if(n!==void 0&&C(n,t))return!0;return!1}function z(e,t){if(e===t)return;let n=e.split(`
`),r=t.split(`
`),i=Math.max(n.length,r.length),a=[];for(let e=0;e<i;e++)n[e]!==r[e]&&a.push(e);return{lines:a,numLinesBefore:n.length,numLinesAfter:r.length,numLines:i}}globalThis.process.platform.startsWith(`win`);const k=Symbol(`clack:cancel`);function q(e){return e===k}function w(e,t){let n=e;n.isTTY&&n.setRawMode(t)}const A=e=>`columns`in e&&typeof e.columns==`number`?e.columns:80,L=e=>`rows`in e&&typeof e.rows==`number`?e.rows:20;function W(t,n,r,i=r,o=r,s){return wrapAnsi(n,A(t??stdout)-r.length,{hard:!0,trim:!1}).split(`
`).map((e,t,n)=>{let a=s?s(e,t):e;return t===0?`${i}${a}`:t===n.length-1?`${o}${a}`:`${r}${a}`}).join(`
`)}let m=class{input;output;_abortSignal;rl;opts;_render;_track=!1;_prevFrame=``;_subscribers=new Map;_cursor=0;state=`initial`;error=``;value;userInput=``;constructor(e,t=!0){let{input:n=stdin,output:r=stdout,render:o,signal:s,...c}=e;this.opts=c,this.onKeypress=this.onKeypress.bind(this),this.close=this.close.bind(this),this.render=this.render.bind(this),this._render=o.bind(this),this._track=t,this._abortSignal=s,this.input=n,this.output=r}unsubscribe(){this._subscribers.clear()}setSubscriber(e,t){let n=this._subscribers.get(e)??[];n.push(t),this._subscribers.set(e,n)}on(e,t){this.setSubscriber(e,{cb:t})}once(e,t){this.setSubscriber(e,{cb:t,once:!0})}emit(e,...t){let n=this._subscribers.get(e)??[],r=[];for(let e of n)e.cb(...t),e.once&&r.push(()=>n.splice(n.indexOf(e),1));for(let e of r)e()}prompt(){return new Promise(e=>{if(this._abortSignal){if(this._abortSignal.aborted)return this.state=`cancel`,this.close(),e(k);this._abortSignal.addEventListener(`abort`,()=>{this.state=`cancel`,this.close()},{once:!0})}this.rl=E.createInterface({input:this.input,tabSize:2,prompt:``,escapeCodeTimeout:50,terminal:!0}),this.rl.prompt(),this.opts.initialUserInput!==void 0&&this._setUserInput(this.opts.initialUserInput,!0),this.input.on(`keypress`,this.onKeypress),w(this.input,!0),this.output.on(`resize`,this.render),this.render(),this.once(`submit`,()=>{this.output.write(import_src.cursor.show),this.output.off(`resize`,this.render),w(this.input,!1),e(this.value)}),this.once(`cancel`,()=>{this.output.write(import_src.cursor.show),this.output.off(`resize`,this.render),w(this.input,!1),e(k)})})}_isActionKey(e,t){return e===`	`}_shouldSubmit(e,t){return!0}_setValue(e){this.value=e,this.emit(`value`,this.value)}_setUserInput(e,t){this.userInput=e??``,this.emit(`userInput`,this.userInput),t&&this._track&&this.rl&&(this.rl.write(this.userInput),this._cursor=this.rl.cursor)}_clearUserInput(){this.rl?.write(null,{ctrl:!0,name:`u`}),this._setUserInput(``)}onKeypress(e,t){if(this._track&&t.name!==`return`&&(t.name&&this._isActionKey(e,t)&&this.rl?.write(null,{ctrl:!0,name:`h`}),this._cursor=this.rl?.cursor??0,this._setUserInput(this.rl?.line)),this.state===`error`&&(this.state=`active`),t?.name&&(!this._track&&h.aliases.has(t.name)&&this.emit(`cursor`,h.aliases.get(t.name)),h.actions.has(t.name)&&this.emit(`cursor`,t.name)),e&&(e.toLowerCase()===`y`||e.toLowerCase()===`n`)&&this.emit(`confirm`,e.toLowerCase()===`y`),this.emit(`key`,e?.toLowerCase(),t),t?.name===`return`&&this._shouldSubmit(e,t)){if(this.opts.validate){let e=this.opts.validate(this.value);e&&(this.error=e instanceof Error?e.message:e,this.state=`error`,this.rl?.write(this.userInput))}this.state!==`error`&&(this.state=`submit`)}C([e,t?.name,t?.sequence],`cancel`)&&(this.state=`cancel`),(this.state===`submit`||this.state===`cancel`)&&this.emit(`finalize`),this.render(),(this.state===`submit`||this.state===`cancel`)&&this.close()}close(){this.input.unpipe(),this.input.removeListener(`keypress`,this.onKeypress),this.output.write(`
`),w(this.input,!1),this.rl?.close(),this.rl=void 0,this.emit(`${this.state}`,this.value),this.unsubscribe()}restoreCursor(){let t=wrapAnsi(this._prevFrame,process.stdout.columns,{hard:!0,trim:!1}).split(`
`).length-1;this.output.write(import_src.cursor.move(-999,t*-1))}render(){let t=wrapAnsi(this._render(this)??``,process.stdout.columns,{hard:!0,trim:!1});if(t!==this._prevFrame){if(this.state===`initial`)this.output.write(import_src.cursor.hide);else{let e=z(this._prevFrame,t),n=L(this.output);if(this.restoreCursor(),e){let r=Math.max(0,e.numLinesAfter-n),i=Math.max(0,e.numLinesBefore-n),a=e.lines.find(e=>e>=r);if(a===void 0){this._prevFrame=t;return}if(e.lines.length===1){this.output.write(import_src.cursor.move(0,a-i)),this.output.write(import_src.erase.lines(1));let e=t.split(`
`);this.output.write(e[a]),this._prevFrame=t,this.output.write(import_src.cursor.move(0,e.length-a-1));return}else if(e.lines.length>1){if(r<i)a=r;else{let e=a-i;e>0&&this.output.write(import_src.cursor.move(0,e))}this.output.write(import_src.erase.down());let e=t.split(`
`).slice(a);this.output.write(e.join(`
`)),this._prevFrame=t;return}}this.output.write(import_src.erase.down())}this.output.write(t),this.state===`initial`&&(this.state=`active`),this._prevFrame=t}}};var ht=class extends m{get userInputWithCursor(){if(this.state===`submit`)return this.userInput;let e=this.userInput;if(this.cursor>=e.length)return`${this.userInput}\u2588`;let t=e.slice(0,this.cursor),[r,...i]=e.slice(this.cursor);return`${t}${styleText(`inverse`,r)}${i.join(``)}`}get cursor(){return this._cursor}constructor(e){super({...e,initialUserInput:e.initialUserInput??e.initialValue}),this.on(`userInput`,e=>{this._setValue(e)}),this.on(`finalize`,()=>{this.value||=e.defaultValue,this.value===void 0&&(this.value=``)})}};export{W,h,ht,m,q};