/**
 * Compatibility Layer for Node.js 0.8+
 * Local to this package - contains only needed functions.
 */ import fs from 'fs';
import Module from 'module';
import os from 'os';
import path from 'path';
// Use existing require in CJS, or createRequire in ESM (Node 12.2+)
const _require = typeof require === 'undefined' ? Module.createRequire(import.meta.url) : require;
export function homedir() {
    return typeof os.homedir === 'function' ? os.homedir() : require('homedir-polyfill')();
}
export function tmpdir() {
    return typeof os.tmpdir === 'function' ? os.tmpdir() : require('os-shim').tmpdir();
}
/**
 * String.prototype.endsWith wrapper for Node.js 0.8+
 * - Uses native endsWith on Node 4.0+ / ES2015+
 * - Falls back to lastIndexOf on Node 0.8-3.x
 */ const hasEndsWith = typeof String.prototype.endsWith === 'function';
export function stringEndsWith(str, search, position) {
    if (hasEndsWith) return str.endsWith(search, position);
    const len = position === undefined ? str.length : position;
    return str.lastIndexOf(search) === len - search.length;
}
/**
 * Recursive mkdir for Node.js 0.8+
 */ export function mkdirpSync(dir) {
    const mkdirp = _require('mkdirp-classic');
    mkdirp.sync(dir);
}
/**
 * Recursive rm for Node.js 0.8+
 */ export function rmSync(dir) {
    const safeRmSync = _require('fs-remove-compat').safeRmSync;
    safeRmSync(dir);
}
export function readdirWithTypes(dir) {
    const names = fs.readdirSync(dir);
    return names.map((name)=>{
        const fullPath = path.join(dir, name);
        let stat;
        try {
            stat = fs.statSync(fullPath);
        } catch (_e) {
            // If stat fails, treat as non-directory
            return {
                name: name,
                isDirectory: ()=>false
            };
        }
        return {
            name: name,
            isDirectory: ()=>stat.isDirectory()
        };
    });
}
/**
 * Object.assign wrapper for Node.js 0.8+
 * - Uses native Object.assign on Node 4.0+
 * - Falls back to manual property copy on Node 0.8-3.x
 */ const hasObjectAssign = typeof Object.assign === 'function';
const _hasOwnProperty = Object.prototype.hasOwnProperty;
export function objectAssign(target, source) {
    if (hasObjectAssign) return Object.assign(target, source);
    for(const key in source){
        if (_hasOwnProperty.call(source, key)) target[key] = source[key];
    }
    return target;
}
