<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue';
import N8nFormInput from '../N8nFormInput';
import type { IFormInput } from '../../types';
import ResizeObserver from '../ResizeObserver';
import type { EventBus } from '../../utils';
import { createEventBus } from '../../utils';

export type FormInputsProps = {
	inputs?: IFormInput[];
	eventBus?: EventBus;
	columnView?: boolean;
	verticalSpacing?: '' | 'xs' | 's' | 'm' | 'l' | 'xl';
	teleported?: boolean;
	tagSize?: 'small' | 'medium';
};

type Value = string | number | boolean | null | undefined;

const props = withDefaults(defineProps<FormInputsProps>(), {
	inputs: () => [],
	eventBus: createEventBus,
	columnView: false,
	verticalSpacing: '',
	teleported: true,
	tagSize: 'small',
});

const emit = defineEmits({
	update: (_: { name: string; value: Value }) => true,
	'update:modelValue': (_: Record<string, Value>) => true,
	submit: (_: Record<string, Value>) => true,
	ready: (_: boolean) => true,
});

const showValidationWarnings = ref(false);
const values = reactive<Record<string, Value>>({});
const validity = ref<Record<string, boolean>>({});

const filteredInputs = computed(() => {
	return props.inputs.filter((input) =>
		typeof input.shouldDisplay === 'function' ? input.shouldDisplay(values) : true,
	);
});

const isReadyToSubmit = computed(() => {
	return Object.values(validity.value).every((valid) => !!valid);
});

watch(isReadyToSubmit, (ready) => {
	emit('ready', ready);
});

function onUpdateModelValue(name: string, value: Value) {
	values[name] = value;
	emit('update', { name, value });
	emit('update:modelValue', values);
}

function onValidate(name: string, isValid: boolean) {
	validity.value = {
		...validity.value,
		[name]: isValid,
	};
}

function onSubmit() {
	showValidationWarnings.value = true;

	if (!isReadyToSubmit.value) {
		return;
	}

	const toSubmit = filteredInputs.value.reduce<Record<string, Value>>((valuesToSubmit, input) => {
		if (values[input.name]) {
			valuesToSubmit[input.name] = values[input.name];
		}
		return valuesToSubmit;
	}, {});

	emit('submit', toSubmit);
}

onMounted(() => {
	for (const input of props.inputs) {
		if ('initialValue' in input) {
			values[input.name] = input.initialValue;
		}
	}

	if (props.eventBus) {
		props.eventBus.on('submit', onSubmit);
	}
});
</script>

<template>
	<ResizeObserver :breakpoints="[{ bp: 'md', width: 500 }]">
		<template #default="{ bp }">
			<div :class="bp === 'md' || columnView ? $style.grid : $style.gridMulti">
				<div
					v-for="(input, index) in filteredInputs"
					:key="input.name"
					:class="{ [`mt-${verticalSpacing}`]: verticalSpacing && index > 0 }"
				>
					<n8n-text
						v-if="input.properties.type === 'info'"
						color="text-base"
						tag="div"
						:size="input.properties.labelSize"
						:align="input.properties.labelAlignment"
						class="form-text"
					>
						{{ input.properties.label }}
					</n8n-text>
					<N8nFormInput
						v-else
						v-bind="input.properties"
						:name="input.name"
						:label="input.properties.label || ''"
						:model-value="values[input.name]"
						:data-test-id="input.name"
						:show-validation-warnings="showValidationWarnings"
						:teleported="teleported"
						:tag-size="tagSize"
						@update:model-value="(value) => onUpdateModelValue(input.name, value as Value)"
						@validate="(value) => onValidate(input.name, value)"
						@enter="onSubmit"
					/>
				</div>
			</div>
		</template>
	</ResizeObserver>
</template>

<style lang="scss" module>
.grid {
	display: grid;
	grid-row-gap: var(--spacing-s);
	grid-column-gap: var(--spacing-2xs);
}

.gridMulti {
	composes: grid;
	grid-template-columns: repeat(2, 1fr);
}
</style>
