import request from './install';
import { PREFIX, formatRecord, formatData } from './tool';
export default Behavior({
  lifetimes: {
    created() { this._i18n_init(false); },
  },
  pageLifetimes: {
    created() { this._i18n_init(true); },
  },
  methods: {
    async _i18n_init(page?: boolean) {
      const [gather, reset] = formatRecord(this.data[PREFIX]);
      const app = getApp({ allowDefault: true });
      if (app.i18n == null) {
        if (reset) this.setData(reset);
        app.i18n = await request();
      }
      if (app.i18n != null) {
        const i18n = formatData(gather, (key) => app.i18n?.[key]);
        if (i18n) this.setData(i18n);
      } else this.setData(formatData(gather));
    },
  }
});