
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { localize } from '../../../../nls.js';
import { ExtensionsRegistry } from '../../extensions/common/extensionsRegistry.js';
import { Extensions } from '../../../../platform/theme/common/colorUtils.js';
import '../../../../platform/theme/common/colors/baseColors.js';
import '../../../../platform/theme/common/colors/chartsColors.js';
import '../../../../platform/theme/common/colors/editorColors.js';
import '../../../../platform/theme/common/colors/inputColors.js';
import '../../../../platform/theme/common/colors/listColors.js';
import '../../../../platform/theme/common/colors/menuColors.js';
import '../../../../platform/theme/common/colors/minimapColors.js';
import '../../../../platform/theme/common/colors/miscColors.js';
import '../../../../platform/theme/common/colors/quickpickColors.js';
import '../../../../platform/theme/common/colors/searchColors.js';
import { Color } from '../../../../base/common/color.js';
import { Registry } from '../../../../platform/registry/common/platform.js';
import { Disposable } from '../../../../base/common/lifecycle.js';
import { Extensions as Extensions$1 } from '../../extensionManagement/common/extensionFeatures.js';
import { SyncDescriptor } from '../../../../platform/instantiation/common/descriptors.js';
import { MarkdownString } from '../../../../base/common/htmlContent.js';
import { registerWorkbenchContribution2, WorkbenchPhase } from '../../../common/contributions.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';

const colorRegistry = ( Registry.as(Extensions.ColorContribution));
const colorReferenceSchema = colorRegistry.getColorReferenceSchema();
const colorIdPattern = '^\\w+[.\\w+]*$';
const configurationExtPoint = ExtensionsRegistry.registerExtensionPoint({
    extensionPoint: 'colors',
    jsonSchema: {
        description: ( localize(12144, 'Contributes extension defined themable colors')),
        type: 'array',
        items: {
            type: 'object',
            properties: {
                id: {
                    type: 'string',
                    description: ( localize(12145, 'The identifier of the themable color')),
                    pattern: colorIdPattern,
                    patternErrorMessage: ( localize(
                        12146,
                        'Identifiers must only contain letters, digits and dots and can not start with a dot'
                    )),
                },
                description: {
                    type: 'string',
                    description: ( localize(12147, 'The description of the themable color')),
                },
                defaults: {
                    type: 'object',
                    properties: {
                        light: {
                            description: ( localize(
                                12148,
                                'The default color for light themes. Either a color value in hex (#RRGGBB[AA]) or the identifier of a themable color which provides the default.'
                            )),
                            type: 'string',
                            anyOf: [
                                colorReferenceSchema,
                                { type: 'string', format: 'color-hex' }
                            ]
                        },
                        dark: {
                            description: ( localize(
                                12149,
                                'The default color for dark themes. Either a color value in hex (#RRGGBB[AA]) or the identifier of a themable color which provides the default.'
                            )),
                            type: 'string',
                            anyOf: [
                                colorReferenceSchema,
                                { type: 'string', format: 'color-hex' }
                            ]
                        },
                        highContrast: {
                            description: ( localize(
                                12150,
                                'The default color for high contrast dark themes. Either a color value in hex (#RRGGBB[AA]) or the identifier of a themable color which provides the default. If not provided, the `dark` color is used as default for high contrast dark themes.'
                            )),
                            type: 'string',
                            anyOf: [
                                colorReferenceSchema,
                                { type: 'string', format: 'color-hex' }
                            ]
                        },
                        highContrastLight: {
                            description: ( localize(
                                12151,
                                'The default color for high contrast light themes. Either a color value in hex (#RRGGBB[AA]) or the identifier of a themable color which provides the default. If not provided, the `light` color is used as default for high contrast light themes.'
                            )),
                            type: 'string',
                            anyOf: [
                                colorReferenceSchema,
                                { type: 'string', format: 'color-hex' }
                            ]
                        }
                    },
                    required: ['light', 'dark']
                }
            }
        }
    }
});
class ColorExtensionPoint {
    constructor() {
        configurationExtPoint.setHandler((extensions, delta) => {
            for (const extension of delta.added) {
                const extensionValue = extension.value;
                const collector = extension.collector;
                if (!extensionValue || !Array.isArray(extensionValue)) {
                    collector.error(( localize(12152, "'configuration.colors' must be a array")));
                    return;
                }
                const parseColorValue = (s, name) => {
                    if (s.length > 0) {
                        if (s[0] === '#') {
                            return Color.Format.CSS.parseHex(s);
                        }
                        else {
                            return s;
                        }
                    }
                    collector.error(( localize(
                        12153,
                        "{0} must be either a color value in hex (#RRGGBB[AA] or #RGB[A]) or the identifier of a themable color which provides the default.",
                        name
                    )));
                    return Color.red;
                };
                for (const colorContribution of extensionValue) {
                    if (typeof colorContribution.id !== 'string' || colorContribution.id.length === 0) {
                        collector.error(( localize(12154, "'configuration.colors.id' must be defined and can not be empty")));
                        return;
                    }
                    if (!colorContribution.id.match(colorIdPattern)) {
                        collector.error(( localize(
                            12155,
                            "'configuration.colors.id' must only contain letters, digits and dots and can not start with a dot"
                        )));
                        return;
                    }
                    if (typeof colorContribution.description !== 'string' || colorContribution.id.length === 0) {
                        collector.error(( localize(
                            12156,
                            "'configuration.colors.description' must be defined and can not be empty"
                        )));
                        return;
                    }
                    const defaults = colorContribution.defaults;
                    if (!defaults || typeof defaults !== 'object' || typeof defaults.light !== 'string' || typeof defaults.dark !== 'string') {
                        collector.error(( localize(
                            12157,
                            "'configuration.colors.defaults' must be defined and must contain 'light' and 'dark'"
                        )));
                        return;
                    }
                    if (defaults.highContrast && typeof defaults.highContrast !== 'string') {
                        collector.error(( localize(
                            12158,
                            "If defined, 'configuration.colors.defaults.highContrast' must be a string."
                        )));
                        return;
                    }
                    if (defaults.highContrastLight && typeof defaults.highContrastLight !== 'string') {
                        collector.error(( localize(
                            12159,
                            "If defined, 'configuration.colors.defaults.highContrastLight' must be a string."
                        )));
                        return;
                    }
                    colorRegistry.registerColor(colorContribution.id, {
                        light: parseColorValue(defaults.light, 'configuration.colors.defaults.light'),
                        dark: parseColorValue(defaults.dark, 'configuration.colors.defaults.dark'),
                        hcDark: parseColorValue(defaults.highContrast ?? defaults.dark, 'configuration.colors.defaults.highContrast'),
                        hcLight: parseColorValue(defaults.highContrastLight ?? defaults.light, 'configuration.colors.defaults.highContrastLight'),
                    }, colorContribution.description);
                }
            }
            for (const extension of delta.removed) {
                const extensionValue = extension.value;
                for (const colorContribution of extensionValue) {
                    colorRegistry.deregisterColor(colorContribution.id);
                }
            }
        });
    }
}
class ColorDataRenderer extends Disposable {
    constructor() {
        super(...arguments);
        this.type = 'table';
    }
    shouldRender(manifest) {
        return !!manifest.contributes?.colors;
    }
    render(manifest) {
        const colors = manifest.contributes?.colors || [];
        if (!colors.length) {
            return { data: { headers: [], rows: [] }, dispose: () => { } };
        }
        const headers = [
            ( localize(12160, "ID")),
            ( localize(12161, "Description")),
            ( localize(12162, "Dark Default")),
            ( localize(12163, "Light Default")),
            ( localize(12164, "High Contrast Default")),
        ];
        const toColor = (colorReference) => colorReference[0] === '#' ? ( Color.fromHex(colorReference)) : undefined;
        const rows = ( colors.sort((a, b) => a.id.localeCompare(b.id))
            .map(color => {
            return [
                ( new MarkdownString()).appendMarkdown(`\`${color.id}\``),
                color.description,
                toColor(color.defaults.dark) ?? ( new MarkdownString()).appendMarkdown(`\`${color.defaults.dark}\``),
                toColor(color.defaults.light) ?? ( new MarkdownString()).appendMarkdown(`\`${color.defaults.light}\``),
                toColor(color.defaults.highContrast) ?? ( new MarkdownString()).appendMarkdown(`\`${color.defaults.highContrast}\``),
            ];
        }));
        return {
            data: {
                headers,
                rows
            },
            dispose: () => { }
        };
    }
}
( Registry.as(Extensions$1.ExtensionFeaturesRegistry)).registerExtensionFeature({
    id: 'colors',
    label: ( localize(12165, "Colors")),
    access: {
        canToggle: false
    },
    renderer: ( new SyncDescriptor(ColorDataRenderer)),
});
let ColorExtensionPointWorkbenchContribution = class ColorExtensionPointWorkbenchContribution {
    static { this.ID = 'workbench.contrib.colorExtensionPoint'; }
    constructor(instantiationService) {
        this.instantiationService = instantiationService;
        this.instantiationService.createInstance(ColorExtensionPoint);
    }
};
ColorExtensionPointWorkbenchContribution = ( __decorate([
    ( __param(0, IInstantiationService))
], ColorExtensionPointWorkbenchContribution));
registerWorkbenchContribution2(ColorExtensionPointWorkbenchContribution.ID, ColorExtensionPointWorkbenchContribution, WorkbenchPhase.BlockStartup);

export { ColorExtensionPoint };
