import dayjs from 'dayjs';
export const getTips = (key: string, lang?: string) => {
  const tip = keysTips.find((item) => item.key === key);
  if (tip) {
    if (lang === 'en') {
      return tip.enTips;
    }
    return tip.tips;
  }
  return '';
};
export const keysTips = [
  {
    key: 'share',
    tips: `共享设置
    1. 设置公共可以直接访问
    2. 设置受保护需要登录后访问
    3. 设置私有只有自己可以访问。\n
    受保护可以设置密码，设置访问的用户名。切换共享状态后，需要重新设置密码和用户名。 不设置，默认是只能自己访问。`,
    enTips: `1. Set public to directly access
    2. Set protected to access after login
    3. Set private to access only yourself.
    Protected can set a password and set the username for access. After switching the shared state, you need to reset the password and username. If not set, it defaults to only being accessible to yourself.`,
  },
  {
    key: 'content-type',
    tips: `内容类型，设置文件的内容类型。默认不要修改。`,
    enTips: `Content type, set the content type of the file. Default do not modify.`,
  },
  {
    key: 'app-source',
    tips: `应用来源，上传方式。默认不要修改。`,
    enTips: `App source, upload method. Default do not modify.`,
  },
  {
    key: 'cache-control',
    tips: `缓存控制，设置文件的缓存控制。默认不要修改。`,
    enTips: `Cache control, set the cache control of the file. Default do not modify.`,
  },
  {
    key: 'password',
    tips: `密码，设置文件的密码。不设置默认是所有人都可以访问。`,
    enTips: `Password, set the password of the file. If not set, it defaults to everyone can access.`,
  },
  {
    key: 'usernames',
    tips: `用户名，设置文件的用户名。不设置默认是所有人都可以访问。`,
    enTips: `Username, set the username of the file. If not set, it defaults to everyone can access.`,
    parse: (value: string) => {
      if (!value) {
        return [];
      }
      return value.split(',');
    },
    stringify: (value: string[]) => {
      if (!value) {
        return '';
      }
      return value.join(',');
    },
  },
  {
    key: 'expiration-time',
    tips: `过期时间，设置文件的过期时间。不设置默认是永久。`,
    enTips: `Expiration time, set the expiration time of the file. If not set, it defaults to permanent.`,
    parse: (value: Date) => {
      if (!value) {
        return null;
      }
      return dayjs(value);
    },
    stringify: (value?: dayjs.Dayjs) => {
      if (!value) {
        return '';
      }
      return value.toISOString();
    },
  },
];
export class KeyParse {
  static parse(metadata: Record<string, any>) {
    const keys = Object.keys(metadata);
    const newMetadata = {};
    keys.forEach((key) => {
      const tip = keysTips.find((item) => item.key === key);
      if (tip && tip.parse) {
        newMetadata[key] = tip.parse(metadata[key]);
      } else {
        newMetadata[key] = metadata[key];
      }
    });
    return newMetadata;
  }
  static stringify(metadata: Record<string, any>) {
    const keys = Object.keys(metadata);
    const newMetadata = {};
    keys.forEach((key) => {
      const tip = keysTips.find((item) => item.key === key);
      if (tip && tip.stringify) {
        newMetadata[key] = tip.stringify(metadata[key]);
      } else {
        newMetadata[key] = metadata[key];
      }
    });
    return newMetadata;
  }
}
