import test from 'node:test';
import assert from 'node:assert';
import { xyz65, rgb, formatCss } from '../src/index.js';

test('xyz65', t => {
	/*
		These should theoretically be equal to the Xn, Yn, and Zn values
		from xyz65/constants.js, but there's a small rounding error for `y`.
	 */
	assert.deepEqual(
		xyz65('white'),
		{
			mode: 'xyz65',
			x: 0.9504559270516715,
			y: 0.9999999999999999,
			z: 1.0890577507598784
		},
		'white'
	);

	assert.deepEqual(xyz65('black'), { mode: 'xyz65', x: 0, y: 0, z: 0 });
	assert.deepEqual(
		xyz65('red'),
		{
			mode: 'xyz65',
			x: 0.4123907992659593,
			y: 0.2126390058715102,
			z: 0.0193308187155918
		},
		'red'
	);
	assert.deepEqual(
		xyz65('#00cc0080'),
		{
			mode: 'xyz65',
			x: 0.21591920006651102,
			y: 0.43183840013302205,
			z: 0.071973066688837,
			alpha: 0.5019607843137255
		},
		'#00cc0080'
	);
});

test('color(xyz)', t => {
	assert.deepEqual(xyz65('color(xyz 1 0 0 / 0.25)'), {
		x: 1,
		y: 0,
		z: 0,
		alpha: 0.25,
		mode: 'xyz65'
	});
	assert.deepEqual(xyz65('color(xyz 0% 50% 0.5 / 25%)'), {
		x: 0,
		y: 0.5,
		z: 0.5,
		alpha: 0.25,
		mode: 'xyz65'
	});
});

test('color(xyz-d65)', t => {
	assert.deepEqual(xyz65('color(xyz-d65 1 0 0 / 0.25)'), {
		x: 1,
		y: 0,
		z: 0,
		alpha: 0.25,
		mode: 'xyz65'
	});
	assert.deepEqual(xyz65('color(xyz-d65 0% 50% 0.5 / 25%)'), {
		x: 0,
		y: 0.5,
		z: 0.5,
		alpha: 0.25,
		mode: 'xyz65'
	});
});

test('color(--xyz-d65)', t => {
	assert.deepEqual(xyz65('color(--xyz-d65 1 0 0 / 0.25)'), undefined);
});

test('formatCss', t => {
	assert.equal(
		formatCss('color(xyz 0% 50% 0.5 / 25%)'),
		'color(xyz-d65 0 0.5 0.5 / 0.25)'
	);
});

test('missing components', t => {
	assert.ok(rgb('color(xyz-d65 none 0.5 none)'), 'xyz65 to rgb is ok');
	assert.deepEqual(
		rgb('color(xyz-d65 none 0.5 none)'),
		rgb('color(xyz-d65 0 0.5 0')
	);
	assert.ok(xyz65('rgb(none 100 20)'), 'rgb to xyz65 is ok');
	assert.deepEqual(xyz65('rgb(none 100 20)'), xyz65('rgb(0 100 20)'));
});
