/*
 * Copyright (c) 2018, 2025, Oracle and/or its affiliates.
 * Licensed under The Universal Permissive License (UPL), Version 1.0
 * as shown at https://oss.oracle.com/licenses/upl/
 */
const Colors=require("./colors");const wcwidth=require("wcwidth");const CR="\r";const NL="\n";var Spinner=function(opts){this._options={text:"",textColor:"yellow",spincolor:"white",frame:[124,47,45,92],interval:100,hideCursor:true,gap:" ",stream:process.stderr};opts=this._mergeOpts(this._options,opts);this._textColor=opts.textColor;this._hideTheCursor=opts.hideCursor!==false;this._interval=opts.interval||100;this._stream=opts.stream;this._frame=opts.frame;this._gap=opts.gap;for(var i=0;i<opts.frame.length;i++){opts.frame[i]=String.fromCharCode(opts.frame[i])}this._frameIndex=0;this._canDo=!process.stdout.isTTY&&process.stderr.isTTY;this._lineCount=0;this._linesToClear=0;this._count=0};Spinner.prototype.start=function(text){if(!this._canDo){return}this.setText(text);if(this._hideTheCursor){this._hideCursor()}};Spinner.prototype.stop=function(text){if(!this._canDo){return}var opts;clearInterval(this._timer);this._timer=null;opts=this._options;this.setText(text);this._frameIndex=0;this.clear();if(this._hideTheCursor){this._showCursor()}};Spinner.prototype.setText=function(text){if(!this._canDo){return}text=typeof text==="string"?this._getNextFrameChar()+this._gap+text:"";text=this._setTextColor(text);const columns=this._stream.columns||80;this._lineCount=_stripAnsi("--"+text).split("\n").reduce((count,line)=>{return count+Math.max(1,Math.ceil(wcwidth(line)/columns))},0);_tslice(this)};Spinner.prototype._startTimer=function(){var opts=this._options;opts.stream.write(this._getNextFrameChar()+opts.gap+opts.text);this._timer=setInterval(_tslice,opts.interval,this)};function _tslice(self){self.clear();self._stream.write(self._text);self._linesToClear=self._lineCount};Spinner.prototype._getNextFrameChar=function(){var opts=this._options;this._frameIndex++;this._frameIndex=this._frameIndex%this._frame.length;return this._frame[this._frameIndex]};Spinner.prototype._hideCursor=function(){this._stream.write("\x1B[?25h");this._stream.write("\x1B[?25l")};Spinner.prototype._showCursor=function(){this._stream.write("\x1B[?25h")};Spinner.prototype._mergeOpts=function(optsTo,optsFrom){var prop;if(optsFrom){for(prop in optsFrom){if(optsFrom.hasOwnProperty(prop)){optsTo[prop]=optsFrom[prop]}}}return optsTo};Spinner.prototype.clear=function(){for(let i=0;i<this._linesToClear;i++){if(i>0){this._stream.moveCursor(0,-1)}this._stream.clearLine();this._stream.cursorTo(0)}this._linesToClear=0};Spinner.prototype._setTextColor=function(text){this._text=this._textColor==="yellow"?Colors.YELLOW(text):text;return this._text};function _stripAnsi(text){return typeof text==="string"?text.replace(_ansiRegex(),""):text};function _ansiRegex(){const pattern=["[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)","(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"].join("|");return new RegExp(pattern,"g")};module.exports=Spinner;