/**
 * Deprecated FAQ block versions and migrations.
 *
 * Migrates pre-v1 / stub attributes (missing ids, title→heading) into the
 * current schemaVersion=1 contract without losing visible content.
 */

import save from './save';
import { migrateToV1 } from './utils/migrate';

export { migrateToV1 };

/**
 * Minimal save used only for deprecated matching (attributes-only).
 *
 * @return {null} Null — content is attribute-driven.
 */
function deprecatedSave() {
	return null;
}

const deprecated = [
	{
		attributes: {
			schemaVersion: { type: 'number', default: 0 },
			title: { type: 'string', default: '' },
			heading: { type: 'string', default: '' },
			intro: { type: 'string', default: '' },
			items: { type: 'array', default: [] },
			displayMode: { type: 'string', default: 'accordion' },
			stylePreset: { type: 'string', default: 'accordion-cards' },
			openFirstItem: { type: 'boolean', default: false },
			showQuestionNumbers: { type: 'boolean', default: false },
			colorPreset: { type: 'string', default: 'blue' },
			density: { type: 'string', default: 'comfortable' },
			sharpCorners: { type: 'boolean', default: false },
		},
		supports: {
			html: false,
			align: ['wide', 'full'],
			anchor: true,
		},
		migrate: migrateToV1,
		isEligible(attributes) {
			if (!attributes || typeof attributes !== 'object') {
				return true;
			}
			if ((attributes.schemaVersion || 0) < 1) {
				return true;
			}
			if (typeof attributes.title === 'string' && attributes.title !== '') {
				return true;
			}
			const items = Array.isArray(attributes.items) ? attributes.items : [];
			return items.some((item) => !item || !item.id);
		},
		save: deprecatedSave,
	},
	{
		attributes: {
			schemaVersion: { type: 'number', default: 1 },
			heading: { type: 'string', default: '' },
			intro: { type: 'string', default: '' },
			items: { type: 'array', default: [] },
			displayMode: { type: 'string', default: 'accordion' },
			stylePreset: { type: 'string', default: 'accordion-cards' },
			openFirstItem: { type: 'boolean', default: false },
			showQuestionNumbers: { type: 'boolean', default: false },
			colorPreset: { type: 'string', default: 'blue' },
			density: { type: 'string', default: 'comfortable' },
			sharpCorners: { type: 'boolean', default: false },
		},
		supports: {
			html: false,
			align: ['wide', 'full'],
			anchor: true,
		},
		save,
		isEligible() {
			return false;
		},
	},
];

export default deprecated;
