// import type { LockInfo } from '#/store';
import type { Nullable } from "@/typings/global";
import { defineStore } from "pinia";
import piniaPersistConfig from "@/config/piniaPersist";
import { HiprintState } from "../interface/index";

interface dwHiprinter {
  id: string;
  printid: number;
  printer: HiprintState.printerParams | null;
  // copies?: number;
}

interface HiprinterState {
  printerInfo: dwHiprinter[];
}

/**
 * @description 记录本地打印机设置：
 */
export const useHiprinterStore = defineStore({
  id: "longjoe-hiprinter",
  state: (): HiprinterState => ({
    printerInfo: []
  }),
  getters: {},
  actions: {
    getPrinter(id: string, printid?: number | undefined) {
      let obj = this.printerInfo?.filter(item => item.id === id && (printid === undefined || item.printid === printid));
      return obj ?? [];
    },
    setPrinter(id: string, printid: number, printer: any, copies?: any) {
      let index = this.printerInfo?.findIndex(item => item.id === id && item.printid === printid) ?? -1;
      if (index > -1) {
        let _item = this.printerInfo[index];
        this.printerInfo?.splice(index, 1, {
          ..._item,
          printer: { ..._item.printer, ...{ printer: printer, copies: copies ?? _item.printer?.copies } }
        });
      } else {
        this.printerInfo?.push({ id, printid, printer: { printer: printer, copies: copies ?? 1 } });
      }
    },
    delPrinter(id: string, printid: number) {
      let index = this.printerInfo?.findIndex(item => item.id === id && item.printid === printid) ?? -1;
      if (index > -1) {
        this.printerInfo?.splice(index, 1);
      }
    }
  },
  persist: piniaPersistConfig("longjoe-hiprinter")
});
