import apiFetch from '@wordpress/api-fetch' import { CredentialsResponse } from './models' export declare interface MybringKey { webShopId: string apiKey: string } const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i const base64Regex = /^[A-Za-z0-9+/]+={0,2}$/ export const parseMybringKey = (key: string): MybringKey | undefined => { const [webShopId, apiKey] = key.split(':') return apiKey !== undefined && uuidRegex.test(webShopId) && base64Regex.test(apiKey) ? { webShopId: webShopId.trim(), apiKey: apiKey.trim() } : undefined } export const connectToMybring = ({ webShopId, apiKey, }: MybringKey): Promise => apiFetch({ path: 'posten-bring-checkout/credentials', method: 'POST', data: { apikey: apiKey, webshop_id: webShopId }, }) export const isKeyAccepted = ({ validCredentials, validWebshopId, accessToWebshop, }: CredentialsResponse): boolean => validCredentials && validWebshopId && accessToWebshop