import { defineStore } from "pinia";
import ajax from "@/plugins/ajax.js";
import { successAlert, confirmAlert } from "@/components/Mudur-Ui-Kit/alert.js";

export const useSettingsStore = defineStore("SettingsStore", {
  state: () => ({
    apiKey: window.gcargo.api_key || "",
    accountDetails: window.gcargo.account_details || {},
    settingsUrl: window.gcargo.settings_url || "",
    allOrderStatuses: window.gcargo.all_order_statuses || {},
    shipmentSetting: window.gcargo.shipment_setting || {},
    returnedSetting: window.gcargo.returned_setting || {},
    packageSetting: window.gcargo.package_setting || {},
    wcStatusSettings: window.gcargo.order_status_settings || {},
    autoShipmentSettings: window.gcargo.auto_shipment_settings || {},
    otherSettings: window.gcargo.other_settings || {},
    gateSettings: window.gcargo.gate_settings || [],
  }),
  actions: {
    saveApiKey: async (value) => {
      await ajax.post("save_api_key", { data: { api_key: value } });
      successAlert({ message: window.gcargo.strings.en.api_key_saved });
    },
    deleteApiKey: async () => {
      confirmAlert({
        title: window.gcargo.strings.en.cancel_title,
        message: window.gcargo.strings.en.delete_api_key_confirm,
        onConfirm: async () => {
          await ajax.post("delete_api_key");
          successAlert({ message: window.gcargo.strings.en.api_key_deleted });
          window.location.reload();
        },
      });
    },
    updateShipmentSetting: async (context) => {
      await ajax.post("update_shipment_setting", context);
      successAlert({ message: window.gcargo.strings.en.settings_updated });
    },
    updateReturnedSetting: async (context) => {
      await ajax.post("update_returned_setting", context);
      successAlert({ message: window.gcargo.strings.en.settings_updated });
    },
    updatePackageSetting: async (context) => {
      await ajax.post("update_package_setting", context);
      successAlert({ message: window.gcargo.strings.en.settings_updated });
    },
    updateOtherSetting: async (context) => {
      await ajax.post("update_other_settings", { data: context });
      successAlert({ message: window.gcargo.strings.en.settings_updated });
      window.location.reload();
    },
    updateAutoShipmentSetting: async (context) => {
      await ajax.post("update_auto_shipment_settings", { data: context });
      successAlert({ message: window.gcargo.strings.en.settings_updated });
    },
    updateWcStatusSettings: async (statusSettings) => {
      await ajax.post("update_wc_order_settings", { data: statusSettings });
      successAlert({ message: window.gcargo.strings.en.settings_updated });
    },
    updateWcCheckoutSettings: async (checkoutSettings) => {
      await ajax.post("update_wc_checkout_settings", {
        data: checkoutSettings,
      });
      successAlert({ message: window.gcargo.strings.en.settings_updated });
    },
    updateGateSettings: async (context) => {
      await ajax.post("update_gate_settings", context);
      successAlert({ message: window.gcargo.strings.en.settings_updated });
    },
  },
});
