import { TSVueIcons } from 'v2/Icon/Icon.vue.d';
import { TSButtonProps } from 'v2/Button/Button.vue.d';
import { ClassComponent } from '../ts-helpers';

export type ScannerErrorCode =
  | 'DRIVER_NOT_FOUND'
  | 'DEVICE_NOT_CONNECTED'
  | 'DEVICE_DISCONNECTED'
  | 'NOT_SUPPORTED_SYSTEM'
  | 'HARDWARE_NOT_SYNCHRONIZED';

export type ScannerErrorMessage = {
  title: string;
  detail: string;
};

export type Device = {
  jenisDevice: string;
  serialNumber: string;
  API: string;
  rfidScan: boolean;
  qrScan: boolean;
  name: string;
};

export type BaseRegisteredDevice = {
  _id: string;
  serialNumber: string;
  name: string;
};

export type RegisteredDevice = {
  _id: string;
  imageSmall: string;
  imageBig: string;
  serialNumber: string;
  status: string;
  lastReportDate: string;
  createdAt: string;
  updatedAt: string;
  group: null;
  name: string;
};

export type DeviceList = Device[];

export interface ButtonScanProps extends TSButtonProps {
  /**
   * Set custom button label.
   *
   * @default Scan Type
   */
  label?: string;
  /**
   * Set custom button icon.
   *
   * @default 'qr' | 'rfid'
   */
  icon?: TSVueIcons;
  /**
   * Scpecify the scan type
   */
  type?: 'RFID' | 'QR';
  /**
   * Automatically switch between RFID and QR scan types during the scanning process.
   * This allows the scan type to change dynamically based on the validation of the scanned code.
   *
   * The scan type will alternate between RFID and QR based on the validation result:
   * - If the current scan is valid, it proceeds with the next scan type.
   * - If the scan is invalid, it will re-scan using the previous scan type.
   *
   * This behavior continues in a loop until a stop condition is met, typically triggered by the parent component.
   *
   * Note: Only work with single Scan
   *
   * @default false
   * @type {boolean}
   *
   * @example
   * // Example usage in a component:
   * <Button :auto-switch="true" />
   *
   * In this case, scanning will automatically switch between RFID and QR based on the validation results.
   */
  autoSwitch?: boolean;

  /**
   * Do validation when a tag is scanned, if valid, it will emits scan, otherwise re-scanning.
   * when need to show toast on validation failed, set the toast group to `scan-error-validation`
   *
   * @param type
   * @param code
   * @param serialNumber
   * @returns
   */
  scanValidation?: (
    type: 'RFID' | 'QR',
    code: string,
    serialNumber: string,
  ) => Promise<boolean>;

  /**
   * The value of the input (tag).
   *
   * @deprecated use `onScan` instead.
   */
  modelValue?: string;
  /**
   *  Whether the scanner is in bulk mode.
   *
   * @toto add support bulk scan
   */
  bulk?: boolean;
  /**
   *  The id of the button element.
   */
  id?: string;
  /**
   * Wether the button should be disabled.
   */
  disabled?: boolean;
  /**
   *  Whether the scanner is a powerbank.
   *
   * @deprecated
   */
  powerbank?: boolean;
  /**
   * Display the label only (wihout icon).
   *
   * @default false
   */
  labelOnly?: boolean;
  /**
   * Base z-index for dialog of scan
   *
   * @default 0
   */
  baseZIndex?: number;
  size: 'small' | 'large';
}

export type ButtonScanEmits = {
  'update:modelValue': [tag: string];
  'connect': [];
  'connected': [device?: Device];
  'scan': [tag: string, deviceSerialNumber: string];
  'stop': [];
  'error': [error: unknown | Event];
  'beforeStartScan': [];
};

export type ButtonScanExposes = {
  onBeforeStartScan: () => void;
  startScan: () => void;
  stopScan: (afterScan?: boolean) => void;
};

/**
 * **TSVue v2 - ButtonScan**
 *
 * _ButtonScan is component for scanning RFID or QR Tag._
 *
 * --- ---
 * ![TSVue](https://ik.imagekit.io/kurniadev/TS-HEAD-BLACK.png)
 *
 * @group Component
 */
declare class ButtonScan extends ClassComponent<
  ButtonScanProps,
  ButtonScanEmits,
  Record<string, unknown>
> {
  /**
   * Method to stop the scan process.
   */
  stopScan: (afterScan?: boolean) => void;
}

export default ButtonScan;
