import replace from '@rollup/plugin-replace';
import copy from 'rollup-plugin-copy';
import {string} from 'rollup-plugin-string';

import packageJSON from './package.json' with {type: 'json'};
import manifestChrome from './src/manifest/chrome.json' with {type: 'json'};
import manifestCommon from './src/manifest/common.json' with {type: 'json'};
import manifestFirefox from './src/manifest/firefox.json' with {type: 'json'};
import manifestOpera from './src/manifest/opera.json' with {type: 'json'};
import manifestSafari from './src/manifest/safari.json' with {type: 'json'};

const BANNER = `/*!
 * HotPocket by BTHLabs (https://hotpocket.app/)
 * Copyright 2025-present BTHLabs <contact@bthlabs.pl> (https://bthlabs.pl/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */`;

const ENV = process.env['NODE_ENV'] || 'development';
const IS_PRODUCTION = (ENV === 'production');
const TARGET = process.env['HOTPOCKET_EXTENSION_TARGET'] || 'safari';

const PLUGINS = [
  string({
    include: [
      'src/content/templates/*.html',
    ],
  }),
  replace({
    include: /\.js$/,
    preventAssignment: true,
    values: {
      '__HOTPOCKET_EXTENSION_ENV__': JSON.stringify(ENV),
      '__HOTPOCKET_EXTENSION_VERSION__': JSON.stringify(packageJSON.version),
    },
  }),
];

const manifestJsonOutputPlugin = () => {
  return {
    name: 'manifest-json',
    renderChunk: async (code, chunk, outputOptions) => {
      let result = {...manifestCommon};

      if (TARGET === 'safari') {
        result = {
          ...result,
          ...manifestSafari,
        };
      } else if (TARGET == 'chrome') {
        result = {
          ...result,
          ...manifestChrome,
        };
      } else if (TARGET == 'firefox') {
        result = {
          ...result,
          ...manifestFirefox,
        };
      } else if (TARGET == 'opera') {
        result = {
          ...result,
          ...manifestOpera,
        };
      }

      result.version = packageJSON.version;

      if (IS_PRODUCTION === false) {
        result.name = '__MSG_extension_name_development__';
        result.action.default_title = '__MSG_extension_name_development__';
      }

      return JSON.stringify(result, null, 2);
    },
  };
};

let OUTPUT_PATH = `dist/${TARGET}`;
if (TARGET === 'safari') {
  OUTPUT_PATH = '../apple/Shared (Extension)/Resources';
} else if (IS_PRODUCTION === true) {
  OUTPUT_PATH = `dist/${TARGET}-production`;
}

export default [
  {
    input: `src/content/${TARGET}.js`,
    output: {
      file: `${OUTPUT_PATH}/content-bundle.js`,
      format: 'iife',
      banner: BANNER,
      sourcemap: false,
    },
    plugins: [
      ...PLUGINS,
      copy({
        targets: [
          {
            src: 'assets/_locales',
            dest: OUTPUT_PATH,
          },
          {
            src: 'assets/images',
            dest: OUTPUT_PATH,
          },
          {
            src: [
              'src/content/options.html',
              'src/content/options.js',
              'src/content/preauth.html',
              'src/content/preauth.js',
            ],
            dest: OUTPUT_PATH,
          },
        ],
      }),
    ],
  },
  {
    input: `src/background/${TARGET}.js`,
    output: {
      file: `${OUTPUT_PATH}/background-bundle.js`,
      format: 'iife',
      banner: BANNER,
      sourcemap: false,
    },
    plugins: PLUGINS,
  },
  {
    input: `src/manifest.js`,
    output: {
      file: `${OUTPUT_PATH}/manifest.json`,
      format: 'es',
      sourcemap: false,
      plugins: [
        manifestJsonOutputPlugin(),
      ],
    },
  }
];
