"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseUserAsCode = exports.parseGroupAsCode = exports.parseCustomRoleAsCode = exports.parseVersionedContentAsCodeDocument = void 0;
const errors_1 = require("../errors");
const base_1 = require("./base");
const asCodeObject = (value, resourceLabel, source) => {
    if (typeof value !== 'object' || value === null || Array.isArray(value)) {
        throw new errors_1.ParameterError(`Invalid ${resourceLabel} file "${source}": expected a YAML object`);
    }
    return value;
};
const requireVersionOne = (value, resourceLabel, source) => {
    if (value.version !== base_1.CONTENT_AS_CODE_VERSION) {
        throw new errors_1.ParameterError(`Invalid ${resourceLabel} file "${source}": expected version ${base_1.CONTENT_AS_CODE_VERSION}`);
    }
};
const requireString = (value, key, resourceLabel, source) => {
    const property = value[key];
    if (typeof property !== 'string' || property.trim().length === 0) {
        throw new errors_1.ParameterError(`Invalid ${resourceLabel} file "${source}": expected a non-empty ${key}`);
    }
    return property;
};
const requireStringArray = (value, key, resourceLabel, source) => {
    const property = value[key];
    if (!Array.isArray(property) ||
        property.some((item) => typeof item !== 'string')) {
        throw new errors_1.ParameterError(`Invalid ${resourceLabel} file "${source}": expected ${key} to be an array of strings`);
    }
    return property;
};
const parseVersionedContentAsCodeDocument = ({ input, source, resourceLabel, contentType, version = base_1.CONTENT_AS_CODE_VERSION, identityKey = 'slug', }) => {
    const value = asCodeObject(input, resourceLabel, source);
    if (value.contentType !== contentType) {
        throw new errors_1.ParameterError(`Invalid contentType in ${resourceLabel} file "${source}": expected "${contentType}"`);
    }
    if (value.version !== version) {
        throw new errors_1.ParameterError(`Invalid ${resourceLabel} file "${source}": expected version ${version}`);
    }
    requireString(value, identityKey, resourceLabel, source);
    return value;
};
exports.parseVersionedContentAsCodeDocument = parseVersionedContentAsCodeDocument;
const parseCustomRoleAsCode = (input, source) => {
    const value = asCodeObject(input, 'custom role', source);
    requireVersionOne(value, 'custom role', source);
    const name = requireString(value, 'name', 'custom role', source);
    if (value.description !== null && typeof value.description !== 'string') {
        throw new errors_1.ParameterError(`Invalid custom role file "${source}": expected description to be a string or null`);
    }
    if (value.level !== 'project' && value.level !== 'organization') {
        throw new errors_1.ParameterError(`Invalid custom role file "${source}": expected level to be project or organization`);
    }
    return {
        version: base_1.CONTENT_AS_CODE_VERSION,
        name,
        description: value.description,
        level: value.level,
        scopes: requireStringArray(value, 'scopes', 'custom role', source),
    };
};
exports.parseCustomRoleAsCode = parseCustomRoleAsCode;
const parseGroupAsCode = (input, source) => {
    const value = asCodeObject(input, 'group', source);
    requireVersionOne(value, 'group', source);
    return {
        version: base_1.CONTENT_AS_CODE_VERSION,
        name: requireString(value, 'name', 'group', source),
        members: requireStringArray(value, 'members', 'group', source),
    };
};
exports.parseGroupAsCode = parseGroupAsCode;
const parseUserRole = (value, source) => {
    const role = asCodeObject(value, 'user role', source);
    if (role.type !== 'system' && role.type !== 'custom') {
        throw new errors_1.ParameterError(`Invalid user file "${source}": expected role type to be system or custom`);
    }
    return {
        type: role.type,
        name: requireString(role, 'name', 'user', source),
    };
};
const parseUserAsCode = (input, source) => {
    const value = asCodeObject(input, 'user', source);
    requireVersionOne(value, 'user', source);
    if (typeof value.disabled !== 'boolean') {
        throw new errors_1.ParameterError(`Invalid user file "${source}": expected disabled to be a boolean`);
    }
    return {
        version: base_1.CONTENT_AS_CODE_VERSION,
        email: requireString(value, 'email', 'user', source),
        disabled: value.disabled,
        role: parseUserRole(value.role, source),
    };
};
exports.parseUserAsCode = parseUserAsCode;
//# sourceMappingURL=parsers.js.map