/**
 * CommonTestAndPreview Selectors
 */

import { createSelector } from 'reselect';

const selectCommonTestAndPreviewDomain = (state) => {
  if (!state) return undefined;
  return state.get('commonTestAndPreview');
};

const makeSelectCommonTestAndPreview = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate
);

const makeSelectCustomers = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate ? substate.get('customers') : undefined,
);

const makeSelectExtractedTags = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('extractedTags')
);

const makeSelectIsSearchingCustomer = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isSearchingCustomer')
);

const makeSelectIsExtractingTags = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isExtractingTags')
);

const makeSelectSearchError = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('searchError')
);

const makeSelectHasSearched = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('hasSearched')
);

const makeSelectApiWarnings = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('apiWarnings')
);

const makeSelectApiErrors = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('apiErrors')
);

const makeSelectPreviewData = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('previewData')
);

const makeSelectIsUpdatingPreview = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isUpdatingPreview')
);

const makeSelectIsSendingTestEmail = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isSendingTestEmail')
);

const makeSelectTags = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('tags'),
);

const makeSelectTestCustomers = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('testCustomers'),
);

const makeSelectIsFetchingTestCustomers = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isFetchingTestCustomers'),
);

const makeSelectTestGroups = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('testGroups'),
);

const makeSelectIsFetchingTestGroups = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isFetchingTestGroups'),
);

const makeSelectMessageMetaConfigId = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('messageMetaConfigId'),
);

const makeSelectIsCreatingMessageMeta = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isCreatingMessageMeta'),
);

const makeSelectPrefilledValues = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => {
    if (!substate) return undefined;
    const prefilledValues = substate.get('prefilledValues');
    if (prefilledValues === null || prefilledValues === undefined) return null;
    return prefilledValues.toJS ? prefilledValues.toJS() : prefilledValues;
  },
);

const makeSelectTestMessageResponse = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('testMessageResponse'),
);

const makeSelectIsSendingTestMessage = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isSendingTestMessage'),
);

const makeSelectUpdatePreviewError = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('updatePreviewError'),
);

const makeSelectUpdatePreviewErrors = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('updatePreviewErrors')?.toJS() || [],
);

const makeSelectFetchPrefilledValuesError = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('fetchPrefilledValuesError'),
);

const makeSelectFetchPrefilledValuesErrors = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('fetchPrefilledValuesErrors')?.toJS() || [],
);

const makeSelectSenderDetailsForChannel = (channel) => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => {
    if (!substate) return [];
    const byChannel = substate.get('senderDetailsByChannel');
    if (!byChannel) return [];
    const list = byChannel.get(channel);
    return list ? list.toJS() : [];
  },
);

const makeSelectSenderDetailsByChannel = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => {
    if (!substate) return {};
    const byChannel = substate.get('senderDetailsByChannel');
    if (!byChannel) return {};
    return byChannel.toJS ? byChannel.toJS() : {};
  },
);

const makeSelectWeCrmAccounts = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => {
    if (!substate) return [];
    const list = substate.get('wecrmAccounts');
    return list ? list.toJS() : [];
  },
);

const makeSelectIsLoadingSenderDetails = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('isLoadingSenderDetails') || false,
);

const makeSelectFetchSenderDetailsError = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('fetchSenderDetailsError'),
);

const makeSelectFetchWeCrmAccountsError = () => createSelector(
  selectCommonTestAndPreviewDomain,
  (substate) => substate.get('fetchWeCrmAccountsError'),
);

export {
  makeSelectCommonTestAndPreview,
  makeSelectCustomers,
  makeSelectExtractedTags,
  makeSelectIsSearchingCustomer,
  makeSelectIsExtractingTags,
  makeSelectSearchError,
  makeSelectHasSearched,
  makeSelectApiWarnings,
  makeSelectApiErrors,
  makeSelectPreviewData,
  makeSelectIsUpdatingPreview,
  makeSelectIsSendingTestEmail,
  makeSelectTags,
  makeSelectTestCustomers,
  makeSelectIsFetchingTestCustomers,
  makeSelectTestGroups,
  makeSelectIsFetchingTestGroups,
  makeSelectMessageMetaConfigId,
  makeSelectIsCreatingMessageMeta,
  makeSelectPrefilledValues,
  makeSelectTestMessageResponse,
  makeSelectIsSendingTestMessage,
  makeSelectUpdatePreviewError,
  makeSelectUpdatePreviewErrors,
  makeSelectFetchPrefilledValuesError,
  makeSelectFetchPrefilledValuesErrors,
  makeSelectSenderDetailsForChannel,
  makeSelectSenderDetailsByChannel,
  makeSelectWeCrmAccounts,
  makeSelectIsLoadingSenderDetails,
  makeSelectFetchSenderDetailsError,
  makeSelectFetchWeCrmAccountsError,
};
