import { RootState } from "app/store";
import { ceil, floor } from "lodash";

export const selectInputValidationErrors = (state: RootState) => {
  // 必須項目のチェック
  const hasErrorEmptyCompany = state.partner.apps.addPartner.companyName === "";
  const hasErrorEmptyCountry = state.partner.apps.addPartner.country === "";
  const hasErrorEmptyAdminName = state.partner.apps.addPartner.adminName === "";
  const hasErrorEmptyEmail = state.partner.apps.addPartner.email === "";

  const emailPattern =
    /^[a-zA-Z0-9!#$%&'_`/=~+\-?^{|}.]+@[a-zA-Z0-9!#$%&'_`/=~+\-?^{|}.]*\.[a-zA-Z0-9!#$%&'_`/=~+\-?^{|}.]*[a-zA-Z]$/;

  const hasErrorIncorrectEmail =
    (state.partner.apps.addPartner.email as string).match(emailPattern)
      ?.length !== 1;

  return {
    hasErrorEmptyCompany,
    hasErrorEmptyCountry,
    hasErrorEmptyAdminName,
    hasErrorEmptyEmail,
    hasErrorIncorrectEmail,
  };
};

// Account Info
export const selectCompany = (state: RootState) =>
  state.partner.apps.addPartner.companyName;
export const selectCountry = (state: RootState) =>
  state.partner.apps.addPartner.country;

// Admin Info
export const selectAdminName = (state: RootState) =>
  state.partner.apps.addPartner.adminName;
export const selectEmail = (state: RootState) =>
  state.partner.apps.addPartner.email;
export const selectIsLoading = (state: RootState) =>
  state.partner.apps.isLoading;

export const selectPartnersInfo = (state: RootState) =>
  state.partner.domain.getPartnersInfo;
export const selectTotal = (state: RootState) =>
  state.partner.domain.getPartnersInfo.total;
export const seletctLimit = (state: RootState) => state.partner.apps.limit;
export const selectOffset = (state: RootState) => state.partner.apps.offset;
export const selectTotalPage = (state: RootState) => {
  const { limit } = state.partner.apps;
  const { total } = state.partner.domain.getPartnersInfo;
  const page = ceil(total / limit);
  return page;
};

export const selectCurrentPage = (state: RootState) => {
  const { limit, offset } = state.partner.apps;
  const page = floor(offset / limit) + 1;
  return page;
};

// delegation
export const selectDelegatedAccountId = (state: RootState) =>
  state.partner.apps.delegatedAccountId;
export const selectDelegatedCompanyName = (state: RootState) =>
  state.partner.apps.delegatedCompanyName;
