/**
 * Puck Configuration for Email Templates
 *
 * Email templates use component types like EmailHeader, EmailText, etc.
 * These must match the types stored in the database.
 */
import type { Config } from '@measured/puck';

// Import email-specific components
import { EmailHeader } from './components/email/EmailHeader';
import { EmailHeading } from './components/email/EmailHeading';
import { EmailText } from './components/email/EmailText';
import { EmailButton } from './components/email/EmailButton';
import { EmailDivider } from './components/email/EmailDivider';
import { EmailSpacer } from './components/email/EmailSpacer';
import { EmailImage } from './components/email/EmailImage';
import { EmailPanel } from './components/email/EmailPanel';
import { EmailTwoColumn } from './components/email/EmailTwoColumn';
import { EmailFooter } from './components/email/EmailFooter';

// Import the combined type
import type { EmailComponentProps } from './components/email/types';

// Create the email config with ALL components
export const emailPuckConfig: Config<EmailComponentProps> = {
  categories: {
    structure: {
      title: 'Structure',
      components: ['EmailHeader', 'EmailFooter'],
    },
    content: {
      title: 'Content',
      components: ['EmailHeading', 'EmailText', 'EmailButton', 'EmailImage'],
    },
    layout: {
      title: 'Layout',
      components: ['EmailSpacer', 'EmailDivider', 'EmailPanel', 'EmailTwoColumn'],
    },
  },
  components: {
    EmailHeader,
    EmailFooter,
    EmailHeading,
    EmailText,
    EmailButton,
    EmailImage,
    EmailSpacer,
    EmailDivider,
    EmailPanel,
    EmailTwoColumn,
  },
};

/**
 * Get email editor config - creates a fresh clone each time.
 */
export function getEmailEditorConfig(): Config<EmailComponentProps> {
  const clonedConfig: Config<EmailComponentProps> = {
    ...emailPuckConfig,
    components: { ...emailPuckConfig.components },
    categories: emailPuckConfig.categories
      ? JSON.parse(JSON.stringify(emailPuckConfig.categories))
      : undefined,
  };
  return clonedConfig;
}

export default emailPuckConfig;
