/*
 * Copyright (C) 2026 Fluxer Contributors
 *
 * This file is part of Fluxer.
 *
 * Fluxer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Fluxer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
 */

import {writeFile} from 'node:fs/promises';
import path from 'node:path';
import {fileURLToPath} from 'node:url';

const allowedChannels = new Set(['stable', 'canary']);
const rawChannel = process.env.BUILD_CHANNEL?.toLowerCase() ?? '';
const channel = allowedChannels.has(rawChannel) ? rawChannel : 'stable';

const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const targetPath = path.resolve(scriptDir, '..', 'src-electron', 'common', 'build-channel.ts');

const fileContent = `/*
 * This file is generated by scripts/set-build-channel.mjs.
 * DO NOT EDIT MANUALLY.
 */

export type BuildChannel = 'stable' | 'canary';

const DEFAULT_BUILD_CHANNEL = '${channel}' as BuildChannel;

const envChannel = process.env.BUILD_CHANNEL?.toLowerCase();
export const BUILD_CHANNEL = (envChannel === 'canary' ? 'canary' : DEFAULT_BUILD_CHANNEL) as BuildChannel;
export const IS_CANARY = BUILD_CHANNEL === 'canary';
export const CHANNEL_DISPLAY_NAME = BUILD_CHANNEL;
`;

await writeFile(targetPath, fileContent, 'utf8');
console.log(`Wrote ${targetPath} with channel '${channel}'`);
