import { readFileSync, existsSync } from 'node:fs';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { pad } from './utils.mjs';
import path from 'node:path';
import JSON5 from 'json5';

//基础配置
//========================================

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

const localConfigPath = path.resolve(__dirname, './_localConfig.json');
const localConfig = existsSync(localConfigPath) ? JSON.parse(readFileSync(localConfigPath, 'utf8')) : {};

//提交更新配置
//========================================

export const config = {
  server: localConfig.updateServer || 'https://update-server1.imengyu.top/',
  submitKey: '',
  appId: 2,
  uploadWebConfig: {
    storageAction: 'override',
    storageProps: {
      overrideMode: 'overrideFiles',
      overrideFiles: [
        'index.html',
      ],
      newFolderNameGenerateType: 'hash',
    },
    deprecateConfig: {
      indexFile: 'index.html',
      updateHtml: readFileSync(path.resolve(__dirname, './deprecate.html'), 'utf8')
    },
  },
  buildWebCommand: 'npm run build:h5', //构建命令
  buildWebOutDir: '../../../dist/build/h5', //构建输出目录。相对于当前文件目录  
  buildWebOptions: {
    skipFiles: [
    ], //打包忽略文件，相对于 buildWebOutDir ，判断开头
  },
  buildWebOutVersionPath: '', //版本号输出目录，输出版本号至文件以供项目使用。相对于当前文件目录
  getCustomConfig: async (param, isApp, versionName) => {
    return {}//额外的自定义配置
  },
  /**
   * 自定义生成Web版本号的方法。
   * @param {Date} now 当前日期
   * @param {Number} lastTodaySubVersion 今天上传的之前版本数量
   * @returns 
   */
  buildWebVersionGenerateCommand: async (now, lastTodaySubVersion) => {
    //生成Web版本号
    const version = `${now.getFullYear().toString().substring(2)}${pad(now.getMonth() + 1, 2)}${pad(now.getDate(), 2)}.${pad(lastTodaySubVersion, 2)}`;
    return version;
  },
  buildAppCallback: async (param, versionCode, versionName, lastTodaySubVersion) => {
    //构建App
    throw new Error('未实现buildAppCallback方法');
  }, 
  buildAppGetUploadFile: async (param, versionCode, versionName) => {
    //获取上传文件路径
    throw new Error('未实现buildAppGetUploadFile方法');
  }, 
  buildAppGetOSSFileName: async (param, versionCode, versionName) => {
    //生成OSS保存路径
    throw new Error('buildAppGetOSSFileName');
  },//构建命令
  buildAppOutDir: './dist', //构建输出目录。相对于当前文件目录
  buildAppGetVersion: async (versionName) => {
    //获取版本号
    throw new Error('未实现buildAppGetVersion方法');
  },
} 