"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.chmodrSync = exports.chmodr = void 0;
const fs = __importStar(require("node:fs"));
const path = __importStar(require("node:path"));
const fsp = __importStar(require("node:fs/promises"));
/* c8 ignore start */
const LCHMOD = 'lchmod' in fs && typeof fs.lchmod === 'function' ? 'lchmod' : 'chmod';
const LCHMODSYNC = 'lchmodSync' in fs && typeof fs.lchmodSync === 'function' ?
    'lchmodSync'
    : 'chmodSync';
/* c8 ignore stop */
// If a party has r, add x
// so that dirs are listable
const dirMode = (mode) => {
    if (mode & 0o400)
        mode |= 0o100;
    if (mode & 0o40)
        mode |= 0o10;
    if (mode & 0o4)
        mode |= 0o1;
    return mode;
};
const chmodrKid = async (p, child, mode) => {
    if (child.isDirectory()) {
        await (0, exports.chmodr)(path.resolve(p, child.name), mode);
        await fsp.chmod(path.resolve(p, child.name), dirMode(mode));
    }
    else {
        await fsp[LCHMOD](path.resolve(p, child.name), mode);
    }
};
const chmodr = async (p, mode) => {
    const children = await fsp
        .readdir(p, { withFileTypes: true })
        .catch(async (er) => {
        if (er.code !== 'ENOTDIR')
            throw er;
        /* c8 ignore start - overly cautious, we only explore dirs */
        await fsp[LCHMOD](p, mode);
    });
    if (!children)
        return;
    /* c8 ignore stop */
    await Promise.all(children.map(async (child) => chmodrKid(p, child, mode)));
    await fsp.chmod(p, dirMode(mode));
};
exports.chmodr = chmodr;
const chmodrKidSync = (p, child, mode) => {
    if (child.isDirectory()) {
        (0, exports.chmodrSync)(path.resolve(p, child.name), mode);
        fs.chmodSync(path.resolve(p, child.name), dirMode(mode));
    }
    else
        fs[LCHMODSYNC](path.resolve(p, child.name), mode);
};
const chmodrSync = (p, mode) => {
    let children;
    try {
        children = fs.readdirSync(p, { withFileTypes: true });
        /* c8 ignore start - overly cautious, we only explore dirs */
    }
    catch (er) {
        if (er.code === 'ENOTDIR') {
            return fs[LCHMODSYNC](p, mode);
        }
        /* c8 ignore stop */
        throw er;
    }
    for (const child of children) {
        chmodrKidSync(p, child, mode);
    }
    return fs.chmodSync(p, dirMode(mode));
};
exports.chmodrSync = chmodrSync;
//# sourceMappingURL=index.js.map