/**
 * Deprecated HowTo block versions and migrations.
 *
 * Migrates Phase 39 stub / pre-v1 attributes into schemaVersion=1
 * without losing visible content.
 */

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

export { migrateToV1 };

/**
 * @return {null} Null — content is attribute-driven.
 */
function deprecatedSave() {
	return null;
}

const deprecated = [
	{
		attributes: {
			schemaVersion: { type: 'number', default: 0 },
			title: { type: 'string', default: '' },
			name: { type: 'string', default: '' },
			summary: { type: 'string', default: '' },
			description: { type: 'string', default: '' },
			steps: { type: 'array', default: [] },
			supplies: { type: 'array', default: [] },
			tools: { type: 'array', default: [] },
			totalTime: { type: 'string', default: '' },
			estimatedCost: {
				type: 'object',
				default: { currency: '', value: '' },
			},
			displayMode: { type: 'string', default: 'numbered' },
			stylePreset: { type: 'string', default: 'clean' },
			colorPreset: { type: 'string', default: 'blue' },
			density: { type: 'string', default: 'comfortable' },
		},
		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;
			}
			if (typeof attributes.summary === 'string' && attributes.summary !== '') {
				return true;
			}
			const steps = Array.isArray(attributes.steps) ? attributes.steps : [];
			return steps.some((step) => !step || !step.id);
		},
		save: deprecatedSave,
	},
	{
		attributes: {
			schemaVersion: { type: 'number', default: 1 },
			name: { type: 'string', default: '' },
			description: { type: 'string', default: '' },
			steps: { type: 'array', default: [] },
			supplies: { type: 'array', default: [] },
			tools: { type: 'array', default: [] },
			totalTime: { type: 'string', default: '' },
			estimatedCost: {
				type: 'object',
				default: { currency: '', value: '' },
			},
			displayMode: { type: 'string', default: 'numbered' },
			stylePreset: { type: 'string', default: 'clean' },
			colorPreset: { type: 'string', default: 'blue' },
			density: { type: 'string', default: 'comfortable' },
		},
		supports: {
			html: false,
			align: ['wide', 'full'],
			anchor: true,
		},
		save,
		isEligible() {
			return false;
		},
	},
];

export default deprecated;
