import { LOGGED_IN_USER } from '../../utils/localStorageKeys'
import Helpers from '@/helpers'
import getCheckoutAgreement from './checkout-agreement'

export default {
  getCheckoutAgreement,
  getUploadsSettings: state => {
    return state.uploadsSettings
  },
  getUploads: state => {
    return state.uploads
  },
  getIcPassportTypeId: state => {
    return state.icPassportValidation.ic_pass_type_id
  },
  getIcPassportNumber: state => {
    return state.icPassportValidation.ic_passport_number
  },
  getUserOnDemandVerificationPass: state => {
    let result = null
    if (state.userOnDemandVerification.exists !== undefined) {
      result = !state.userOnDemandVerification.exists
    }
    return result
  },
  getExternalApiClassicCrossSponsoringVerificationPass: state => {
    let result = null
    if (Object.keys(state.externalAPIs.classicCrossSponsoringVerification).length > 0) {
      if (
        (state.externalAPIs.classicCrossSponsoringVerification.result &&
          !state.externalAPIs.classicCrossSponsoringVerification.api_error) ||
        (!state.externalAPIs.classicCrossSponsoringVerification.result &&
          state.externalAPIs.classicCrossSponsoringVerification.api_error)
      ) {
        // Skip checking if classic api server db error or server down
        result = true
      } else {
        result = false
      }
    }
    return result
  },
  getExternalApiClassicCrossSponsoringVerificationHasApiError: state => {
    return Object.keys(state.externalAPIs.classicCrossSponsoringVerification).length > 0
      ? !state.externalAPIs.classicCrossSponsoringVerification.result &&
          state.externalAPIs.classicCrossSponsoringVerification.api_error
      : false
  },
  getAutoValidationActivateStatus: state => {
    return state.autoValidationActivate
  },
  getIcPassportValidationPassed: (state, getters) => {
    let exist = getters.getUserOnDemandVerificationPass
    let verification = getters.getExternalApiClassicCrossSponsoringVerificationPass
    return (exist === null || exist) && (verification === null || verification)
  },
  getPrivateChannelNameWithUserID: () => channel => {
    return channel + '.' + JSON.parse(localStorage.getItem(LOGGED_IN_USER)).id
  },
  getIcPassportValidationInProgress: (state, getters) => {
    return getters.isLoading('userOnDemandVerification') || getters.isLoading('classicCrossSponsoringVerification')
  },
  getExperimentalFeatureSettingByKey: state => key => {
    return state.experimentalFeature[key] || false
  },
  getExperimentalFeatureSettings: state => state.experimentalFeature,
}
