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;
    };
})();
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "vscode-uri", "path"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.getSchemaTypeName = getSchemaTypeName;
    exports.getSchemaRefTypeTitle = getSchemaRefTypeTitle;
    exports.getSchemaTitle = getSchemaTitle;
    exports.isPrimitiveType = isPrimitiveType;
    exports.isAnyOfAllOfOneOfType = isAnyOfAllOfOneOfType;
    const vscode_uri_1 = require("vscode-uri");
    const path = __importStar(require("path"));
    function getSchemaTypeName(schema) {
        const closestTitleWithType = schema.type && schema.closestTitle;
        if (schema.title) {
            return schema.title;
        }
        if (schema.$id) {
            return getSchemaRefTypeTitle(schema.$id);
        }
        if (schema.$ref || schema._$ref) {
            return getSchemaRefTypeTitle(schema.$ref || schema._$ref);
        }
        return Array.isArray(schema.type)
            ? schema.type.join(' | ')
            : closestTitleWithType
                ? schema.type.concat('(', schema.closestTitle, ')')
                : schema.type || schema.closestTitle; //object
    }
    /**
     * Get type name from reference url
     * @param $ref reference to the same file OR to the another component OR to the section in another component:
     * `schema-name.schema.json` -> schema-name
     * `custom-scheme://shared-schema.json#/definitions/SomeType` -> SomeType
     * `custom-scheme://schema-name.schema.json` -> schema-name
     * `shared-schema.schema.json#/definitions/SomeType` -> SomeType
     * `file:///Users/user/Documents/project/schemas/schema-name.schema.json` -> schema-name
     * `#/definitions/SomeType` -> SomeType
     * `#/definitions/io.k8s.api.apps.v1.DaemonSetSpec` => io.k8s.api.apps.v1.DaemonSetSpec
     * `file:///default_schema_id.yaml` => default_schema_id.yaml
     * test: https://regex101.com/r/ZpuXxk/1
     */
    function getSchemaRefTypeTitle($ref) {
        const match = $ref.match(/^(?:.*\/)?(.*?)(?:\.schema\.json)?$/);
        let type = !!match && match[1];
        if (!type) {
            type = 'typeNotFound';
            console.error(`$ref (${$ref}) not parsed properly`);
        }
        return type;
    }
    function getSchemaTitle(schema, url) {
        const uri = vscode_uri_1.URI.parse(url);
        let baseName = path.basename(uri.fsPath);
        if (!path.extname(uri.fsPath)) {
            baseName += '.json';
        }
        if (Object.getOwnPropertyDescriptor(schema, 'name')) {
            return Object.getOwnPropertyDescriptor(schema, 'name').value + ` (${baseName})`;
        }
        else if (schema.title) {
            return schema.description ? schema.title + ' - ' + schema.description + ` (${baseName})` : schema.title + ` (${baseName})`;
        }
        return baseName;
    }
    function isPrimitiveType(schema) {
        return schema.type !== 'object' && !isAnyOfAllOfOneOfType(schema);
    }
    function isAnyOfAllOfOneOfType(schema) {
        return !!(schema.anyOf || schema.allOf || schema.oneOf);
    }
});
//# sourceMappingURL=schemaUtils.js.map