/**
 * EN: Style Dictionary configuration for GoodGo design token pipeline.
 *     Transforms token JSON → CSS custom properties + C# constants.
 *     Single source of truth: packages/design-tokens/tokens/*.json
 *
 * VI: Cấu hình Style Dictionary cho pipeline design token GoodGo.
 *     Chuyển đổi JSON token → CSS custom properties + C# constants.
 *     Nguồn duy nhất: packages/design-tokens/tokens/*.json
 *
 * Usage: node --input-type=module style-dictionary.config.mjs
 * Or:    npx style-dictionary build --config style-dictionary.config.mjs
 */

import StyleDictionary from 'style-dictionary';

// ─── Custom transform: camelCase → kebab-case for CSS custom properties ───────
StyleDictionary.registerTransform({
  name: 'name/goodgo/css',
  type: 'name',
  transform: (token) => {
    // Flatten path, convert camelCase to kebab-case, join with '-'
    return token.path
      .map((part) => part.replace(/([A-Z])/g, '-$1').toLowerCase())
      .join('-');
  },
});

// ─── Custom formatter: C# static class with const string fields ───────────────
StyleDictionary.registerFormat({
  name: 'csharp/goodgo-tokens',
  format: ({ dictionary }) => {
    const indent = '        ';
    const lines = dictionary.allTokens.map((token) => {
      const name = token.path
        .map((p) => p.charAt(0).toUpperCase() + p.slice(1))
        .join('');
      const comment = token.comment ? ` // ${token.comment}` : '';
      return `${indent}public const string ${name} = "${token.value}";${comment}`;
    });

    return [
      '// <auto-generated>',
      '//   EN: This file is auto-generated by the Style Dictionary pipeline.',
      '//       Do NOT edit manually. Update tokens/*.json and re-run the build.',
      '//   VI: File này được tạo tự động bởi Style Dictionary pipeline.',
      '//       KHÔNG chỉnh sửa thủ công. Cập nhật tokens/*.json và build lại.',
      '// </auto-generated>',
      '',
      'namespace GoodGo.BlazorUi.DesignTokens;',
      '',
      '/// <summary>',
      '/// EN: Auto-generated design token constants. Maps 1:1 with CSS custom properties.',
      '/// VI: Hằng số design token tự động. Ánh xạ 1:1 với CSS custom properties.',
      '/// </summary>',
      'public static class DesignTokens',
      '{',
      ...lines,
      '}',
      '',
    ].join('\n');
  },
});

// ─── Main config ──────────────────────────────────────────────────────────────
export default {
  source: ['tokens/**/*.json'],

  platforms: {
    /**
     * EN: CSS custom properties — consumed by app.css and component stylesheets.
     *     Output: apps/web-client-tpos-net/src/WebClientTpos.Client/wwwroot/css/tokens.generated.css
     * VI: CSS custom properties — dùng trong app.css và stylesheet component.
     */
    css: {
      transformGroup: 'css',
      transforms: ['name/goodgo/css', 'attribute/cti'],
      prefix: 'gds',
      buildPath: '../../apps/web-client-tpos-net/src/WebClientTpos.Client/wwwroot/css/',
      files: [
        {
          destination: 'tokens.generated.css',
          format: 'css/variables',
          options: {
            outputReferences: false,
            selector: ':root',
          },
          filter: (token) => token.attributes?.category !== 'primitive',
        },
      ],
    },

    /**
     * EN: C# constants — consumed by Blazor components for type-safe token access.
     *     Output: packages/blazor-ui/DesignTokens/DesignTokens.g.cs
     * VI: C# constants — dùng trong Blazor component để truy cập token an toàn.
     */
    csharp: {
      transforms: ['attribute/cti'],
      buildPath: '../blazor-ui/DesignTokens/',
      files: [
        {
          destination: 'DesignTokens.g.cs',
          format: 'csharp/goodgo-tokens',
          filter: (token) => token.attributes?.category !== 'primitive',
        },
      ],
    },
  },
};
