"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.EMPTY_SCHEMA_URL = exports.CRD_CATALOG_URL = exports.JSON_SCHEMASTORE_URL = exports.DEFAULT_KUBERNETES_SCHEMA_VERSION = void 0;
exports.isKubernetes = isKubernetes;
exports.checkSchemaURI = checkSchemaURI;
exports.getSchemaUrls = getSchemaUrls;
const vscode_uri_1 = require("vscode-uri");
const path = __importStar(require("path"));
const objects_1 = require("./objects");
const paths_1 = require("./paths");
exports.DEFAULT_KUBERNETES_SCHEMA_VERSION = 'v1.34.1';
exports.JSON_SCHEMASTORE_URL = 'https://www.schemastore.org/api/json/catalog.json';
exports.CRD_CATALOG_URL = 'https://raw.githubusercontent.com/datreeio/CRDs-catalog/main';
exports.EMPTY_SCHEMA_URL = 'vscode://schemas/empty';
const KUBERNETES_SCHEMA_URL_PATTERN = /^https:\/\/raw\.githubusercontent\.com\/yannh\/kubernetes-json-schema\/master\/((?:v(\d+)\.(\d+)\.(\d+))-standalone-strict)\/all\.json$/;
function isKubernetes(uri) {
    if (uri.trim().toLowerCase() === 'kubernetes')
        return true;
    return KUBERNETES_SCHEMA_URL_PATTERN.test(uri);
}
function checkSchemaURI(workspaceFolders, workspaceRoot, uri, telemetry, kubernetesVersion) {
    const k8sKeywordUsed = uri.trim().toLowerCase() === 'kubernetes';
    if (k8sKeywordUsed || KUBERNETES_SCHEMA_URL_PATTERN.test(uri)) {
        telemetry.send({ name: 'yaml.schema.configured', properties: { kubernetes: true } });
        if (k8sKeywordUsed) {
            return `https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/${kubernetesVersion ?? exports.DEFAULT_KUBERNETES_SCHEMA_VERSION}-standalone-strict/all.json`;
        }
        else {
            return uri;
        }
    }
    else if (path.isAbsolute(uri) || /^[a-z]:[\\/]/i.test(uri)) {
        const localPath = uri.split('#', 2)[0];
        return vscode_uri_1.URI.file(localPath).toString() + uri.substring(localPath.length);
    }
    else if ((0, paths_1.isRelativePath)(uri)) {
        return (0, paths_1.relativeToAbsolutePath)(workspaceFolders, workspaceRoot, uri);
    }
    else {
        return uri;
    }
}
/**
 * Collect all urls of sub schemas
 * @param schema the root schema
 * @returns map url to schema
 */
function getSchemaUrls(schema) {
    const result = new Map();
    if (!schema) {
        return result;
    }
    if (schema.url) {
        if (schema.url.startsWith('schemaservice://combinedSchema/')) {
            addSchemasForOf(schema, result);
        }
        else {
            result.set(schema.url, schema);
        }
    }
    else {
        addSchemasForOf(schema, result);
    }
    return result;
}
function addSchemasForOf(schema, result) {
    if (schema.allOf) {
        addInnerSchemaUrls(schema.allOf, result);
    }
    if (schema.anyOf) {
        addInnerSchemaUrls(schema.anyOf, result);
    }
    if (schema.oneOf) {
        addInnerSchemaUrls(schema.oneOf, result);
    }
}
function addInnerSchemaUrls(schemas, result) {
    for (const subSchema of schemas) {
        if (!(0, objects_1.isBoolean)(subSchema) && subSchema.url && !result.has(subSchema.url)) {
            result.set(subSchema.url, subSchema);
        }
    }
}
//# sourceMappingURL=schemaUrls.js.map