import path from 'path';
import { defineConfig } from 'tsup';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const entry = {
  index: 'index.ts',
  // Existing grouped exports
  'src/constants/index': 'src/constants/index.ts',
  'src/utils/index': 'src/utils/index.ts',
  'src/types/index': 'src/types/index.ts',
  // Granular constants exports
  'src/constants/constants': 'src/constants/constants.ts',
  'src/constants/countries': 'src/constants/countries.ts',
  'src/constants/vkey': 'src/constants/vkey.ts',
  'src/constants/skiPem': 'src/constants/skiPem.ts',
  'src/constants/mockCertificates': 'src/constants/mockCertificates.ts',
  'src/constants/sampleDataHashes': 'src/constants/sampleDataHashes.ts',
  // Granular utils exports
  'src/utils/aadhaar/constants': 'src/utils/aadhaar/constants.ts',
  'src/utils/aadhaar/utils': 'src/utils/aadhaar/utils.ts',
  'src/utils/aadhaar/mockData': 'src/utils/aadhaar/mockData.ts',
  'src/utils/attest': 'src/utils/attest.ts',
  'src/utils/hash': 'src/utils/hash.ts',
  'src/utils/bytes': 'src/utils/bytes.ts',
  'src/utils/trees': 'src/utils/trees.ts',
  'src/utils/proving': 'src/utils/proving.ts',
  'src/utils/scope': 'src/utils/scope.ts',
  'src/utils/appType': 'src/utils/appType.ts',
  'src/utils/date': 'src/utils/date.ts',
  'src/utils/arrays': 'src/utils/arrays.ts',
  'src/utils/types': 'src/utils/types.ts',
  'src/utils/passports/index': 'src/utils/passports/index.ts',
  'src/utils/passports/format': 'src/utils/passports/format.ts',
  'src/utils/passports/mock': 'src/utils/passports/mock.ts',
  'src/utils/passports/validate': 'src/utils/passports/validate.ts',
  'src/utils/passports/dg1': 'src/utils/passports/dg1.ts',
  'src/utils/passports/genMockPassportData': 'src/utils/passports/genMockPassportData.ts',
  'src/utils/passports/genMockIdDoc': 'src/utils/passports/genMockIdDoc.ts',
  'src/utils/passports/passport_parsing/parseDscCertificateData':
    'src/utils/passports/passport_parsing/parseDscCertificateData.ts',
  'src/utils/certificate_parsing/index': 'src/utils/certificate_parsing/index.ts',
  'src/utils/certificate_parsing/elliptic': 'src/utils/certificate_parsing/elliptic.ts',
  'src/utils/certificate_parsing/curves': 'src/utils/certificate_parsing/curves.ts',
  'src/utils/certificate_parsing/oids': 'src/utils/certificate_parsing/oids.ts',
  'src/utils/certificate_parsing/parseCertificateSimple':
    'src/utils/certificate_parsing/parseCertificateSimple.ts',
  'src/utils/circuits/index': 'src/utils/circuits/index.ts',
  'src/utils/circuits/circuitsName': 'src/utils/circuits/circuitsName.ts',
  'src/utils/circuits/formatOutputs': 'src/utils/circuits/formatOutputs.ts',
  'src/utils/circuits/formatInputs': 'src/utils/circuits/formatInputs.ts',
  'src/utils/circuits/uuid': 'src/utils/circuits/uuid.ts',
  'src/utils/contracts/index': 'src/utils/contracts/index.ts',
  'src/utils/contracts/forbiddenCountries': 'src/utils/contracts/forbiddenCountries.ts',
  'src/utils/csca': 'src/utils/csca.ts',
  'src/utils/ofac': 'src/utils/ofac.ts',
  // Level 3 Hash Function Exports
  'src/utils/hash/poseidon': 'src/utils/hash/poseidon.ts',
  'src/utils/hash/sha': 'src/utils/hash/sha.ts',
  'src/utils/hash/custom': 'src/utils/hash/custom.ts',
  // Level 3 Circuit Function Exports
  'src/utils/circuits/dscInputs': 'src/utils/circuits/dscInputs.ts',
  'src/utils/circuits/registerInputs': 'src/utils/circuits/registerInputs.ts',
  'src/utils/circuits/discloseInputs': 'src/utils/circuits/discloseInputs.ts',
  'src/utils/circuits/generateInputs': 'src/utils/circuits/generateInputs.ts',
  'src/utils/circuits/ofacInputs': 'src/utils/circuits/ofacInputs.ts',
  // Level 3 Certificate Function Exports
  'src/utils/certificate_parsing/parseSimple': 'src/utils/certificate_parsing/parseSimple.ts',
  'src/utils/certificate_parsing/parseNode': 'src/utils/certificate_parsing/parseNode.ts',
  'src/utils/certificate_parsing/ellipticInit': 'src/utils/certificate_parsing/ellipticInit.ts',
  'src/utils/certificate_parsing/curveUtils': 'src/utils/certificate_parsing/curveUtils.ts',
  'src/utils/certificate_parsing/oidUtils': 'src/utils/certificate_parsing/oidUtils.ts',
  'src/utils/certificate_parsing/certUtils': 'src/utils/certificate_parsing/certUtils.ts',
  // Level 3 Passport Function Exports
  'src/utils/passports/core': 'src/utils/passports/core.ts',
  'src/utils/passports/commitment': 'src/utils/passports/commitment.ts',
  'src/utils/passports/signature': 'src/utils/passports/signature.ts',
  'src/utils/passports/parsing': 'src/utils/passports/parsing.ts',
  'src/utils/passports/mockGeneration': 'src/utils/passports/mockGeneration.ts',
  'src/utils/passports/mockDsc': 'src/utils/passports/mockDsc.ts',
  'src/utils/passports/passport': 'src/utils/passports/passport.ts',
  // Granular types exports
  'src/types/passport': 'src/types/passport.ts',
  'src/types/app': 'src/types/app.ts',
  'src/types/certificates': 'src/types/certificates.ts',
  'src/types/circuits': 'src/types/circuits.ts',
};

export default defineConfig([
  // ESM build (matches current dist/esm/src structure)
  {
    tsconfig: './tsconfig.json',
    entry: entry,
    format: ['esm'],
    outDir: path.resolve(__dirname, 'dist/esm'),
    outExtension: ({ format }) => ({ js: format === 'cjs' ? '.cjs' : '.js' }),
    dts: false, // Generated separately via build:types script
    splitting: false,
    clean: true, // Clean only on first build
    sourcemap: true,
    target: 'es2020',
    platform: 'neutral',
    external: [
      /^@openpassport/,
      /^@zk-email/,
      /^@anon-aadhaar/,
      /^asn1/,
      /^axios/,
      /^buffer/,
      /^chai/,
      /^child_process/,
      /^country-/,
      /^elliptic/,
      /^ethers/,
      /^fs/,
      /^i18n-/,
      /^js-/,
      /^json-/,
      /^jsrsasign/,
      /^node-forge/,
      /^path/,
      /^pkijs/,
      /^poseidon-/,
      /^snarkjs/,
      /^typescript-/,
      /^uuid/,
    ],
  },
  // CJS build (matches current dist/cjs/src structure)
  {
    tsconfig: './tsconfig.cjs.json',
    entry: entry,
    format: ['cjs'],
    outDir: path.resolve(__dirname, 'dist/cjs'),
    outExtension: ({ format }) => ({ js: format === 'cjs' ? '.cjs' : '.js' }),
    dts: false, // Only generate types once (in ESM build)
    splitting: false,
    clean: false, // Don't clean after ESM build
    sourcemap: true,
    target: 'es2020',
    platform: 'neutral',
    external: [
      /^@openpassport/,
      /^@zk-email/,
      /^@anon-aadhaar/,
      /^asn1/,
      /^axios/,
      /^buffer/,
      /^chai/,
      /^child_process/,
      /^country-/,
      /^elliptic/,
      /^ethers/,
      /^fs/,
      /^i18n-/,
      /^js-/,
      /^json-/,
      /^jsrsasign/,
      /^node-forge/,
      /^path/,
      /^pkijs/,
      /^poseidon-/,
      /^snarkjs/,
      /^typescript-/,
      /^uuid/,
    ],
  },
]);
