import {CardType} from "./tree/cards";
import {SelectOption} from "./tree/fields/MultiSelectSearch";
import {ImportableDataFormatServer} from "./tree/export/AppImporter";

export const $ = (window || {} as any).jQuery;
export const CouponsPlus: {
    state: ImportableDataFormatServer;
    userRoles: Record<
        string, // id (eg: administrator)
        string // name (eg: Administrator)
    >;
    shippingMethods: Record<
        string, // id (eg: bacs)
        string // name (eg: Direct Bank Transfer)
    >;
    paymentMethods: Record<
        string, // id (eg: bacs)
        string // name (eg: Direct Bank Transfer)
    >;
    successfulOrderStatuses: Record<
        string, // the order status (eg: wc-completed)
        string // the name (eg: Completed)
    >; // id => name
    components: {
        filters: Record<CardType['type'], CardType>,
        conditions: Record<CardType['type'], CardType>,
        offers: Record<CardType['type'], CardType>,
    },
    /**
     * These are components that are have no implementation, only used as stubs for informational display
     */
    stubs: {
        filters: Record<CardType['type'], CardType>,
        conditions: Record<CardType['type'], CardType>,
        offers: Record<CardType['type'], CardType>,
    },
    cache: SelectOption[],
    attributes: { id: string, name: string, values: { id: string, name: string, slug: string }[] }[],
    WooCommerce: {
        currency: string, // this is the code
        currencyPosition: 'left' | 'right', // I think we also have 'left_space' and 'right_space', or something like that so always use .contains('left') or .contains('right')
    },
    environment: 'development' | 'production',
    urls: {
        store: string,
    },
    compatibility: {
        legacy: {
            isLegacyInstallation: boolean,
        }
    }
} = (window as any).CouponsPlus;

export const urls = CouponsPlus.urls
export const isNew = !CouponsPlus?.state?.grove?.length
export const cache = CouponsPlus?.cache || [];
export const __ = (text: string): string => {
    const wpI18n = (window as any).wp?.i18n;
    if (wpI18n && typeof wpI18n.__ === 'function') {
        return wpI18n.__(text, (window as any).CouponsPlus?.textDomain) ?? text;
    }
    return text;
};
export const getRegisteredComponentsMeta = (window as any)?.CouponsPlusAPI?.getRegisteredComponents || (() => []);
export const getRegisteredCardRenderers = (window as any)?.CouponsPlusAPI?.getRegisteredComponents || (() => {});
export const hasProInstalled = CouponsPlus.implementations['Coupons+ Pro']