
import * as vega from 'vega';
import { chart_data_color_generator } from './data-colors';

// todo: css variables
export function parse_vega_spec(spec: vega.Spec) {
	const config: vega.Config = {
		rect: {
			fill: 'var(--theme-line, currentcolor)',
		},
		text: {
			font: 'var(--font-open-sans, sans-serif)',
			fill: 'var(--theme-text-body, currentcolor)',
		},
		axis: {
			labelFont: 'var(--font-open-sans, sans-serif)',
			labelColor: 'var(--theme-text-body, currentcolor)',
			domainColor: 'var(--theme-line, currentcolor)',
			gridColor: 'var(--theme-line, currentcolor)',
			tickColor: 'var(--theme-line, currentcolor)',
		}
	};

	if (spec.marks) {
		const colors = chart_data_color_generator();

		for (const mark of spec.marks) {
			switch (mark.type) {
				case 'rect':
					mark.encode.enter.fill = { value: colors.next().value };
					break;

				// todo: apply colors to more mark types
			}
		}
	}

	return vega.parse(spec, config);
}
