import { RootState } from "app/store";

export const selectInputValidationErrors = (state: RootState) => {
  const { poNumber, newOrder } = state.license.apps;

  // 必須項目のチェック
  const hasErrorEmptyPoNumber = poNumber === "";
  // newOrderは初期値が入っている＋minvalueが1なので必須チェックは行わない

  const hasErrorIncorrectPoNumber = checkErrorIncorrectPoNumber(poNumber);
  const hasErrorIncorrectNewOrder = checkErrorIncorrectNewOrder(newOrder);

  return {
    hasErrorEmptyPoNumber,
    hasErrorIncorrectPoNumber,
    hasErrorIncorrectNewOrder,
  };
};
export const checkErrorIncorrectPoNumber = (poNumber: string): boolean => {
  // 大文字半角英数字で50文字まで
  const charaTypePattern = /^[A-Z0-9]{1,50}$/;
  const charaType = new RegExp(charaTypePattern).test(poNumber);

  return !charaType;
};
export const checkErrorIncorrectNewOrder = (newOrder: number): boolean => {
  // 0以下の場合はエラー
  if (newOrder <= 0) {
    return true;
  }

  return false;
};

export const selectPoNumber = (state: RootState) => state.license.apps.poNumber;
export const selectNewOrder = (state: RootState) => state.license.apps.newOrder;

export const selectIsLoading = (state: RootState) =>
  state.license.apps.isLoading;
