1import api from '@/store/api';
2import i18n from '@/i18n';
3
4const PowerPolicyStore = {
5  namespaced: true,
6  state: {
7    powerRestoreCurrentPolicy: null,
8    powerRestorePolicies: [],
9  },
10  getters: {
11    powerRestoreCurrentPolicy: (state) => state.powerRestoreCurrentPolicy,
12    powerRestorePolicies: (state) => state.powerRestorePolicies,
13  },
14  mutations: {
15    setPowerRestoreCurrentPolicy: (state, powerRestoreCurrentPolicy) =>
16      (state.powerRestoreCurrentPolicy = powerRestoreCurrentPolicy),
17    setPowerRestorePolicies: (state, powerRestorePolicies) =>
18      (state.powerRestorePolicies = powerRestorePolicies),
19  },
20  actions: {
21    async getPowerRestorePolicies({ commit }) {
22      return await api
23        .get('/redfish/v1/JsonSchemas/ComputerSystem/ComputerSystem.json')
24        .then(
25          ({
26            data: {
27              definitions: { PowerRestorePolicyTypes = {} },
28            },
29          }) => {
30            let powerPoliciesData = PowerRestorePolicyTypes.enum.map(
31              (powerState) => {
32                let desc = `${i18n.t(
33                  `pagePowerRestorePolicy.policies.${powerState}`,
34                )} - ${PowerRestorePolicyTypes.enumDescriptions[powerState]}`;
35                return {
36                  state: powerState,
37                  desc,
38                };
39              },
40            );
41            commit('setPowerRestorePolicies', powerPoliciesData);
42          },
43        );
44    },
45    async getPowerRestoreCurrentPolicy({ commit }) {
46      return await api
47        .get(`${await this.dispatch('global/getSystemPath')}`)
48        .then(({ data: { PowerRestorePolicy } }) => {
49          commit('setPowerRestoreCurrentPolicy', PowerRestorePolicy);
50        })
51        .catch((error) => console.log(error));
52    },
53    async setPowerRestorePolicy({ dispatch }, powerPolicy) {
54      const data = { PowerRestorePolicy: powerPolicy };
55
56      return await api
57        .patch(`${await this.dispatch('global/getSystemPath')}`, data)
58        .then(() => {
59          dispatch('getPowerRestoreCurrentPolicy');
60          return i18n.t('pagePowerRestorePolicy.toast.successSaveSettings');
61        })
62        .catch((error) => {
63          console.log(error);
64          throw new Error(
65            i18n.t('pagePowerRestorePolicy.toast.errorSaveSettings'),
66          );
67        });
68    },
69  },
70};
71
72export default PowerPolicyStore;
73