"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.spinner = exports.ciSpinner = void 0;
exports.getCliSpinners = getCliSpinners;
exports.Spinner = Spinner;
/**
 * @fileoverview CLI spinner utilities for long-running operations.
 * Provides animated progress indicators with CI environment detection.
 */
const ENV_1 = require("./constants/ENV");
const abort_signal_1 = require("./constants/abort-signal");
const strings_1 = require("./strings");
const yocto_spinner_1 = require("../external/@socketregistry/yocto-spinner");
// IMPORTANT: Do not use destructuring here - use direct assignment instead.
// tsgo has a bug that incorrectly transpiles destructured exports, resulting in
// `exports.SomeName = void 0;` which causes runtime errors.
// See: https://github.com/SocketDev/socket-packageurl-js/issues/3
const ObjectHasOwn = Object.hasOwn;
exports.ciSpinner = {
    frames: [''],
    interval: 2147483647,
};
function desc(value) {
    return {
        __proto__: null,
        configurable: true,
        value,
        writable: true,
    };
}
function normalizeText(value) {
    return typeof value === 'string' ? value.trimStart() : '';
}
let _cliSpinners;
/*@__NO_SIDE_EFFECTS__*/
function getCliSpinners(styleName) {
    if (_cliSpinners === undefined) {
        const yoctoFactory = /*@__PURE__*/ require('../external/@socketregistry/yocto-spinner');
        const { constructor: YoctoCtor } = yoctoFactory();
        _cliSpinners = YoctoCtor.spinners;
    }
    if (typeof styleName === 'string' && _cliSpinners) {
        return ObjectHasOwn(_cliSpinners, styleName)
            ? _cliSpinners[styleName]
            : undefined;
    }
    return _cliSpinners;
}
let _Spinner;
let _defaultSpinner;
/*@__NO_SIDE_EFFECTS__*/
function Spinner(options) {
    if (_Spinner === undefined) {
        const { constructor: YoctoCtor } = (0, yocto_spinner_1)();
        /*@__PURE__*/
        _Spinner = class SpinnerClass extends YoctoCtor {
            constructor(options) {
                // eslint-disable-next-line constructor-super
                super({
                    signal: abort_signal_1,
                    ...options,
                });
            }
            #apply(methodName, args) {
                let extras;
                let text = args.at(0);
                if (typeof text === 'string') {
                    extras = args.slice(1);
                }
                else {
                    extras = args;
                    text = '';
                }
                const wasSpinning = this.isSpinning;
                const normalized = normalizeText(text);
                super[methodName](normalized);
                const { incLogCallCountSymbol, lastWasBlankSymbol, logger, } = /*@__PURE__*/ require('./logger.js');
                if (methodName === 'stop') {
                    if (wasSpinning && normalized) {
                        logger[lastWasBlankSymbol]((0, strings_1.isBlankString)(normalized));
                        logger[incLogCallCountSymbol]();
                    }
                }
                else {
                    logger[lastWasBlankSymbol](false);
                    logger[incLogCallCountSymbol]();
                }
                if (extras.length) {
                    logger.log(...extras);
                    logger[lastWasBlankSymbol](false);
                }
                return this;
            }
            #applyAndKeepSpinning(methodName, args) {
                const wasSpinning = this.isSpinning;
                this.#apply(methodName, args);
                if (wasSpinning) {
                    this.start();
                }
                return this;
            }
            debug(...args) {
                const { isDebug } = /*@__PURE__*/ require('./debug.js');
                if (isDebug()) {
                    return this.#applyAndKeepSpinning('info', args);
                }
                return this;
            }
            debugAndStop(...args) {
                const { isDebug } = /*@__PURE__*/ require('./debug.js');
                if (isDebug()) {
                    return this.#apply('info', args);
                }
                return this;
            }
            fail(...args) {
                return this.#applyAndKeepSpinning('error', args);
            }
            failAndStop(...args) {
                return this.#apply('error', args);
            }
            getText() {
                return this.text;
            }
            info(...args) {
                return this.#applyAndKeepSpinning('info', args);
            }
            infoAndStop(...args) {
                return this.#apply('info', args);
            }
            log(...args) {
                return this.#applyAndKeepSpinning('stop', args);
            }
            logAndStop(...args) {
                return this.#apply('stop', args);
            }
            setText(value) {
                this.text = normalizeText(value);
                return this;
            }
            start(...args) {
                if (args.length) {
                    const text = args.at(0);
                    const normalized = normalizeText(text);
                    // We clear this.text on start when `text` is falsy because yocto-spinner
                    // will not clear it otherwise.
                    if (!normalized) {
                        this.setText('');
                    }
                }
                return this.#apply('start', args);
            }
            stop(...args) {
                // We clear this.text on stop because yocto-spinner will not clear it.
                this.setText('');
                return this.#apply('stop', args);
            }
            success(...args) {
                return this.#applyAndKeepSpinning('success', args);
            }
            successAndStop(...args) {
                return this.#apply('success', args);
            }
            warn(...args) {
                return this.#applyAndKeepSpinning('warning', args);
            }
            warnAndStop(...args) {
                return this.#apply('warning', args);
            }
        };
        // Add aliases.
        Object.defineProperties(_Spinner.prototype, {
            error: desc(_Spinner.prototype.fail),
            errorAndStop: desc(_Spinner.prototype.failAndStop),
            warning: desc(_Spinner.prototype.warn),
            warningAndStop: desc(_Spinner.prototype.warnAndStop),
        });
        _defaultSpinner = ENV_1.CI ? exports.ciSpinner : undefined;
    }
    return new _Spinner({
        spinner: _defaultSpinner,
        ...options,
    });
}
exports.spinner = Spinner();
//# sourceMappingURL=spinner.js.map