import React, { Component } from "react";

export interface WatnedPublishBaseP {
  inputWantedRange?: [number, number];
  handleConfirm?: Function;
  currentWanted?: string | number | null;
  viewConfig?: {
    showInputWantedClear?: boolean;
    viewTextOptions?: {
      viewTitle: string;
      imgLabelBottom: string;
      inputTipTop: string;
      numberSuffix: string;
      inputTipBottom: string;
      clearModalTitle?: string;
      clearModalTip?: string;
    };
  };
}

export class WatnedPublishBase<
  T extends WatnedPublishBaseP,
  S
> extends Component<T, S> {
  formatCurrentWanted = (
    value: string | number | null
  ): string | number | null => {
    const { inputWantedRange = [5, 10000] } = this.props;
    if (!value) return null;
    let result: string | number;
    result = value;
    if (parseInt(`${value}`, 10) === value) {
      // 无小数判断
      result = Number(value);
    }
    if (!result) return null;
    if (result > inputWantedRange[1]) result = inputWantedRange[1];
    if (result < inputWantedRange[0]) result = inputWantedRange[0];
    return result;
  };

  formatResult = (value: string | number | null) => {
    if (!value) return null;
    return Number(value).toFixed(2);
  };
}
