"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizePluginMetadataContent = exports.normalizePluginMetadataValue = exports.normalizePluginMetadataPathString = void 0;
const os = __importStar(require("os"));
const path = __importStar(require("path"));
function getPluginPathModule(targetConfigDir, input) {
    return targetConfigDir.includes('\\') || input.includes('\\') ? path.win32 : path.posix;
}
function normalizeTargetConfigDir(targetConfigDir, input) {
    const pathModule = getPluginPathModule(targetConfigDir, input);
    return pathModule.normalize(pathModule === path.win32
        ? targetConfigDir.replace(/\//g, '\\')
        : targetConfigDir.replace(/\\/g, '/'));
}
function normalizePluginMetadataPathString(input, targetConfigDir = path.join(os.homedir(), '.claude')) {
    const match = input.match(/^(.*?)([\\/])(?:\.claude|\.ccs\2shared|\.ccs\2instances\2[^\\/]+)\2plugins(?:(\2.*))?$/);
    if (!match) {
        return input;
    }
    const pathModule = getPluginPathModule(targetConfigDir, input);
    const normalizedTargetConfigDir = normalizeTargetConfigDir(targetConfigDir, input);
    const suffix = match[3] ?? '';
    const suffixSegments = suffix.split(/[\\/]+/).filter(Boolean);
    return pathModule.join(normalizedTargetConfigDir, 'plugins', ...suffixSegments);
}
exports.normalizePluginMetadataPathString = normalizePluginMetadataPathString;
function normalizePluginMetadataValue(value, targetConfigDir) {
    if (typeof value === 'string') {
        const normalized = normalizePluginMetadataPathString(value, targetConfigDir);
        return { normalized, changed: normalized !== value };
    }
    if (Array.isArray(value)) {
        let changed = false;
        const normalized = value.map((item) => {
            const result = normalizePluginMetadataValue(item, targetConfigDir);
            changed = changed || result.changed;
            return result.normalized;
        });
        return { normalized, changed };
    }
    if (value && typeof value === 'object') {
        let changed = false;
        const normalized = Object.fromEntries(Object.entries(value).map(([key, item]) => {
            const result = normalizePluginMetadataValue(item, targetConfigDir);
            changed = changed || result.changed;
            return [key, result.normalized];
        }));
        return { normalized, changed };
    }
    return { normalized: value, changed: false };
}
exports.normalizePluginMetadataValue = normalizePluginMetadataValue;
function normalizePluginMetadataContent(original, targetConfigDir = path.join(os.homedir(), '.claude')) {
    const parsed = JSON.parse(original);
    const result = normalizePluginMetadataValue(parsed, targetConfigDir);
    return result.changed ? JSON.stringify(result.normalized, null, 2) : original;
}
exports.normalizePluginMetadataContent = normalizePluginMetadataContent;
//# sourceMappingURL=plugin-path-normalizer.js.map