import HookPathPort from "../../ports/HookPathPort.js";
function resolveHooksPath(customPath, repoPath, pathUtils) {
    if (customPath.startsWith('/')) {
        return customPath;
    }
    return pathUtils.resolve(repoPath, customPath);
}
export default class PlumbingHookPathAdapter extends HookPathPort {
    _plumbingFactory;
    _path;
    constructor({ plumbingFactory, path, }) {
        super();
        this._plumbingFactory = plumbingFactory;
        this._path = path;
    }
    async resolveHooksDir(repoPath) {
        const plumbing = this._plumbingFactory.create(repoPath);
        const customPath = await this._configGet(plumbing, 'core.hooksPath');
        if (customPath !== null && customPath.length > 0) {
            return resolveHooksPath(customPath, repoPath, this._path);
        }
        const gitDir = await this._gitDir(plumbing);
        if (gitDir !== null && gitDir.length > 0) {
            return this._path.join(this._path.resolve(repoPath, gitDir), 'hooks');
        }
        return this._path.join(repoPath, '.git', 'hooks');
    }
    async _configGet(plumbing, key) {
        try {
            const value = await plumbing.execute({ args: ['config', '--get', key] });
            return value.trim();
        }
        catch {
            return null;
        }
    }
    async _gitDir(plumbing) {
        try {
            const value = await plumbing.execute({ args: ['rev-parse', '--git-dir'] });
            return value.trim();
        }
        catch {
            return null;
        }
    }
}
