import fs from 'node:fs';
/* eslint-disable no-prototype-builtins */
import os from 'node:os';
import path, { dirname } from 'node:path';
import readline from 'node:readline';
import { fileURLToPath } from 'node:url';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore import packageJson from '../../package.json';
const __dirname = dirname(fileURLToPath(import.meta.url));
const splitRE = /\r?\n/;
export const FARM_TARGET_NODE_ENVS = [
    'node',
    'node16',
    'node-legacy',
    'node-next'
];
export const FARM_TARGET_BROWSER_ENVS = [
    'browser',
    'browser-legacy',
    'browser-es2015',
    'browser-es2017',
    'browser-esnext'
];
/* eslint-disable @typescript-eslint/no-use-before-define */
export function isObject(value) {
    return Object.prototype.toString.call(value) === '[object Object]';
}
export function isArray(value) {
    return Array.isArray(value);
}
export function isEmptyObject(obj) {
    if (!obj)
        return true;
    return Reflect.ownKeys(obj).length === 0;
}
export const isUndefined = (obj) => typeof obj === 'undefined';
export const isString = (val) => typeof val === 'string';
export const isNumber = (val) => typeof val === 'number';
export const isEmpty = (array) => !(array && array.length > 0);
export const isSymbol = (val) => typeof val === 'symbol';
export const isWindows = os.platform() === 'win32';
export function pad(source, n = 2) {
    const lines = source.split(splitRE);
    return lines.map((l) => ` `.repeat(n) + l).join(`\n`);
}
export function clearScreen() {
    try {
        const repeatCount = process.stdout.rows - 2;
        const blank = repeatCount > 0 ? '\n'.repeat(repeatCount) : '';
        console.log(blank);
        readline.cursorTo(process.stdout, 0, 0);
        readline.clearScreenDown(process.stdout);
    }
    catch (error) {
        console.error('Failed to clear screen:', error);
    }
}
export const version = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../../package.json')).toString()).version;
export function normalizePath(id) {
    return path.posix.normalize(id);
}
export function normalizeBasePath(basePath) {
    return path.posix.normalize(isWindows ? basePath.replace(/\\/g, '/') : basePath);
}
export function arraify(target) {
    return Array.isArray(target) ? target : [target];
}
export function getFileSystemStats(file) {
    try {
        return fs.statSync(file, { throwIfNoEntry: false });
    }
    catch (error) {
        console.error(`Error accessing file ${file}:`, error);
        return undefined;
    }
}
export function toArray(array) {
    return array ? (Array.isArray(array) ? array : [array]) : [];
}
export function mergeObjects(obj1, obj2) {
    const merged = { ...obj1 };
    Object.keys(obj2).forEach((key) => {
        if (Object.prototype.hasOwnProperty.call(obj2, key)) {
            if (merged.hasOwnProperty(key) &&
                typeof obj2[key] === 'object' &&
                !Array.isArray(obj2[key])) {
                merged[key] = mergeObjects(merged[key], obj2[key]);
            }
            else {
                merged[key] = obj2[key];
            }
        }
    });
    return merged;
}
export async function asyncFlatten(arr) {
    do {
        arr = (await Promise.all(arr)).flat(Infinity);
    } while (arr.some((v) => v?.then));
    return arr;
}
export function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
// prevent node experimental warning
export function preventExperimentalWarning() {
    const defaultEmit = process.emit;
    process.emit = function (...args) {
        if (args[1].name === 'ExperimentalWarning') {
            return undefined;
        }
        return defaultEmit.call(this, ...args);
    };
}
export function mapTargetEnvValue(config) {
    if (FARM_TARGET_NODE_ENVS.includes(config.output.targetEnv)) {
        config.output.targetEnv = 'node';
    }
    else if (FARM_TARGET_BROWSER_ENVS.includes(config.output.targetEnv)) {
        config.output.targetEnv = 'browser';
    }
}
export function tryStatSync(file) {
    try {
        return fs.statSync(file, { throwIfNoEntry: false });
    }
    catch { }
}
//# sourceMappingURL=share.js.map