"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Parameter = exports.Parameters = void 0;
const tslib_1 = require("tslib");
const json_schema_viewer_1 = require("@stoplight/json-schema-viewer");
const types_1 = require("@stoplight/types");
const ui_kit_1 = require("@stoplight/ui-kit");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const lodash_1 = require("lodash");
const React = tslib_1.__importStar(require("react"));
const Provider_1 = require("../../../containers/Provider");
const MarkdownViewer_1 = require("../../MarkdownViewer");
const SectionTitle_1 = require("./SectionTitle");
const numberValidationNames = [
    'minimum',
    'maximum',
    'minLength',
    'maxLength',
    'minItems',
    'maxItems',
    'exclusiveMinimum',
    'exclusiveMaximum',
];
const readableStyles = {
    [types_1.HttpParamStyles.PipeDelimited]: 'Pipe separated values',
    [types_1.HttpParamStyles.SpaceDelimited]: 'Space separated values',
    [types_1.HttpParamStyles.CommaDelimited]: 'Comma separated values',
    [types_1.HttpParamStyles.Simple]: 'Comma separated values',
    [types_1.HttpParamStyles.Matrix]: 'Path style values',
    [types_1.HttpParamStyles.Label]: 'Label style values',
    [types_1.HttpParamStyles.Form]: 'Form style values',
};
const defaultStyle = {
    query: types_1.HttpParamStyles.Form,
    header: types_1.HttpParamStyles.Simple,
    path: types_1.HttpParamStyles.Simple,
    cookie: types_1.HttpParamStyles.Form,
};
exports.Parameters = ({ parameters, parameterType, title, className, icon, }) => {
    const resolveRef = React.useContext(Provider_1.InlineRefResolverContext);
    if (!parameters || !parameters.length)
        return null;
    return (React.createElement("div", { className: classnames_1.default('HttpOperation__Parameters', className) },
        title && React.createElement(SectionTitle_1.SectionTitle, { title: title, icon: icon }),
        lodash_1.sortBy(parameters, ['required', 'name']).map((parameter, index) => {
            var _a;
            const resolvedSchema = ((_a = parameter.schema) === null || _a === void 0 ? void 0 : _a.$ref) && resolveRef
                ? resolveRef({ pointer: parameter.schema.$ref, source: null }, null, {})
                : null;
            return (React.createElement(exports.Parameter, { key: parameter.name, parameter: resolvedSchema ? Object.assign(Object.assign({}, parameter), { schema: resolvedSchema }) : parameter, parameterType: parameterType, className: classnames_1.default('pt-4', {
                    'pb-4': parameters.length - 1 !== index,
                    'border-t border-gray-2 dark:border-gray-6': index > 0,
                }) }));
        })));
};
exports.Parameters.displayName = 'HttpOperation.Parameters';
exports.Parameter = ({ parameter, parameterType, className }) => {
    if (!parameter)
        return null;
    const description = lodash_1.get(parameter, 'description') || lodash_1.get(parameter, 'schema.description');
    const type = lodash_1.get(parameter, 'schema.type', 'unknown');
    const deprecated = lodash_1.get(parameter, 'deprecated') || lodash_1.get(parameter, 'schema.deprecated', false);
    const validations = lodash_1.omitBy(Object.assign(Object.assign({}, lodash_1.omit(parameter, ['name', 'required', 'deprecated', 'description', 'schema', 'style'])), lodash_1.omit(lodash_1.get(parameter, 'schema'), ['description', 'type', 'deprecated'])), value => typeof value === 'object' && lodash_1.isEmpty(value));
    const numberValidations = lodash_1.pick(validations, numberValidationNames);
    const booleanValidations = lodash_1.omit(lodash_1.pickBy(validations, v => ['true', 'false'].includes(String(v))), ['exclusiveMinimum', 'exclusiveMaximum']);
    const keyValueValidations = lodash_1.omit(validations, [...lodash_1.keys(numberValidations), ...lodash_1.keys(booleanValidations)]);
    return (React.createElement("div", { className: classnames_1.default('HttpOperation__Parameter pl-1', className) },
        React.createElement("div", { className: "flex items-center" },
            React.createElement("div", { className: "font-medium font-mono" }, parameter.name),
            React.createElement("div", { className: classnames_1.default('ml-2 text-sm', json_schema_viewer_1.PropertyTypeColors[type]) }, type),
            parameterType !== 'path' && (React.createElement("div", { className: classnames_1.default('ml-2 text-sm', {
                    'text-danger': parameter.required,
                    'opacity-50': !parameter.required,
                }) }, parameter.required ? 'required' : 'optional')),
            React.createElement(NumberValidations, { validations: numberValidations })),
        React.createElement(KeyValueValidations, { validations: keyValueValidations }),
        React.createElement(MarkdownViewer_1.MarkdownViewer, { className: "text-gray-7 dark:text-gray-4 mt-1", markdown: description || '*No description.*' }),
        deprecated || parameter.style || lodash_1.keys(validations).length ? (React.createElement("div", { className: "flex flex-wrap" },
            deprecated && (React.createElement(ui_kit_1.Tag, { className: "mt-2 mr-2", intent: "warning", minimal: true }, "Deprecated")),
            React.createElement(NameValidations, { validations: booleanValidations }),
            parameter.style && defaultStyle[parameterType] !== parameter.style && (React.createElement(ui_kit_1.Tag, { className: "mt-2 mr-2", minimal: true }, readableStyles[parameter.style] || parameter.style)))) : null));
};
exports.Parameter.displayName = 'HttpOperation.Parameter';
const NumberValidations = ({ validations, className }) => (React.createElement(React.Fragment, null, lodash_1.keys(lodash_1.omit(validations, ['exclusiveMinimum', 'exclusiveMaximum'])).map(key => {
    let suffix;
    if (key.includes('Length')) {
        suffix = ' characters';
    }
    else if (key.includes('Items')) {
        suffix = ' items';
    }
    else {
        suffix = '';
    }
    const exclusive = (key === 'minimum' && validations.exclusiveMinimum) || (key === 'maximum' && validations.exclusiveMaximum)
        ? true
        : false;
    const sign = `${key.includes('min') ? '>' : '<'}${exclusive ? '' : '='}`;
    return (React.createElement("div", { key: key, className: classnames_1.default('ml-2 text-sm bp3-running-text', className) },
        React.createElement("code", null, `${sign} ${validations[key]}${suffix}`)));
})));
const KeyValueValidations = ({ validations, className }) => (React.createElement(React.Fragment, null, lodash_1.keys(validations).map(key => {
    return React.createElement(KeyValueValidation, { key: key, name: key, value: validations[key], className: className });
})));
const KeyValueValidation = ({ className, name, value, }) => {
    if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
        return (React.createElement(React.Fragment, null, lodash_1.keys(value).map(key => (React.createElement(KeyValueValidation, { key: key, className: className, name: `${name}.${key}`, value: value[key] })))));
    }
    const validation = Array.isArray(value) ? value : [value];
    return (React.createElement("div", { className: classnames_1.default('text-sm mt-2 bp3-running-text', className) },
        lodash_1.capitalize(name),
        ":",
        validation
            .filter((v) => typeof v !== 'object' || (typeof v === 'object' && v !== null && 'value' in v))
            .map(v => {
            const value = typeof v === 'object' ? v.value : String(v);
            return (React.createElement("code", { className: "ml-1", key: value }, value));
        })));
};
const NameValidations = ({ validations, className }) => (React.createElement(React.Fragment, null, lodash_1.keys(validations)
    .filter(key => validations[key])
    .map(key => {
    return (React.createElement(ui_kit_1.Tag, { key: key, className: classnames_1.default('mt-2 mr-2 capitalize', className), minimal: true }, key));
})));
//# sourceMappingURL=Parameters.js.map