/*
 *
 * Templates actions
 *
 */

import * as types from './constants';
import {
  LINE, WHATSAPP, RCS, ZALO, WEBPUSH,
} from '../CreativesContainer/constants';

export function getAllTemplates(channel, queryParams, intlCopyOf = '') {
  return {
    type: types.GET_ALL_TEMPLATES_REQUEST, channel, queryParams, intlCopyOf,
  };
}

export function resetTemplate() {
  return {
    type: types.RESET_TEMPLATE,
  };
}

export function resetAccount() {
  return {
    type: types.RESET_ACCOUNT,
  };
}

export function deleteTemplate(channel, id) {
  return {
    type: types.DELETE_TEMPLATE_REQUEST, channel, id,
  };
}

export function deleteRcsTemplate(templateName) {
  return {
    type: types.DELETE_RCS_TEMPLATE_REQUEST, templateName,
  };
}

export function getUserList() {
  return {
    type: types.GET_USER_LIST_REQUEST,
  };
}

export function getWeCrmAccounts(source) {
  return {
    type: types.GET_WECRM_ACCOUNTS_REQUEST, source,
  };
}

export function getAccountsSettings() {
  return {
    type: types.GET_ORG_LEVEL_CAMPAIGN_SETTINGS_REQUEST,
  };
}

export function setWeChatAccount(weChatAccount) {
  return {
    type: types.SET_WECHAT_ACCOUNT,
    weChatAccount,
  };
}
export function setChannelAccount(channel, account) {
  const channelTypeMapping = {
    [LINE]: types.SET_LINE_ACCOUNT,
    [WHATSAPP]: types.SET_WHATSAPP_ACCOUNT,
    [ZALO]: types.SET_ZALO_ACCOUNT,
    [WEBPUSH]: types.SET_WEBPUSH_ACCOUNT,
    [RCS]: types.SET_RCS_ACCOUNT,
  };
  
  if (channelTypeMapping[channel]) {
    return {
      type: channelTypeMapping[channel],
      account,
    };
  }
  return {};
}

export function setViberAccount(viberAccount) {
  return {
    type: types.SET_VIBER_ACCOUNT,
    viberAccount,
  };
}

export function setFacebookAccount(faceBookAccount) {
  return {
    type: types.SET_FACEBOOK_ACCOUNT,
    faceBookAccount,
  };
}

export function handleHtmlUpload(selectedTemplate) {
  return {
    type: types.SET_SELECTED_LAYOUT,
    selectedTemplate,
  };
}

export function handleZipUpload(selectedFile, successHandler, errorHandler) {
  return {
    type: types.SEND_ZIPPED_FILE_REQUEST,
    selectedFile,
    successHandler,
    errorHandler,
  };
}

export function getDefaultBeeTemplates() {
  return {
    type: types.GET_DEAFULT_BEE_TEMPLATES_REQUEST,
  };
}

export function setEdmTemplate(template) {
  return {
    type: types.SET_EDM_TEMPLATE,
    template,
  };
}
export function setBEETemplate(template) {
  return {
    type: types.SET_BEE_TEMPLATE,
    template,
  };
}
export function getTemplateDetails(id, channel) {
  return {
    type: types.GET_TEMPLATE_DETAILS_REQUEST,
    id,
    channel,
  };
}

export function resetTemplateData() {
  return {
    type: types.RESET_TEMPLATE_DATA,
  };
}

export function resetUploadData() {
  return {
    type: types.RESET_UPLOAD_DATA,
  };
}

export function resetTemplateStoreData() {
  return {
    type: types.CLEAR_TEMPLATE_STORE_DATA,
  };
}

export function getSenderDetails(data = {}) {
  return {
    type: types.GET_SENDER_DETAILS_REQUEST,
    ...data,
  };
}

export function getCdnTransformationConfig() {
  return {
    type: types.GET_CDN_TRANSFORMATION_CONFIG_REQUEST,
  };
}

export function archiveTemplate(channel, id, successMessage, description) {
  return {
    type: types.ARCHIVE_TEMPLATE_REQUEST, channel, id, successMessage, description 
  };
}

export function unarchiveTemplate(channel, id, successMessage, description) {
  return {
    type: types.UNARCHIVE_TEMPLATE_REQUEST, channel, id, successMessage, description 
  };
}

export function bulkArchiveTemplates(channel, ids, successMessage) {
  return {
    type: types.BULK_ARCHIVE_REQUEST, channel, ids, successMessage 
  };
}

export function bulkUnarchiveTemplates(channel, ids, successMessage) {
  return {
    type: types.BULK_UNARCHIVE_REQUEST, channel, ids, successMessage 
  };
}

export function setArchiveFilter(filter) {
  return { type: types.SET_ARCHIVE_FILTER, filter };
}

export function toggleTemplateSelection(id) {
  return { type: types.TOGGLE_TEMPLATE_SELECTION, id };
}

export function selectAllTemplates(ids) {
  return { type: types.SELECT_ALL_TEMPLATES, ids };
}

export function clearTemplateSelection() {
  return { type: types.CLEAR_TEMPLATE_SELECTION };
}

export function setArchivedMode(isArchived) {
  return { type: types.SET_ARCHIVED_MODE, isArchived };
}
