import { z } from 'zod';
import { DEFAULT_UUID } from './constants.js';
import { v4 as uuidv4 } from 'uuid';
var ShopifyProvider;
(function (ShopifyProvider) {
    ShopifyProvider["KOMI"] = "komi";
    ShopifyProvider["RYE"] = "rye";
})(ShopifyProvider || (ShopifyProvider = {}));
//Massaging done temporarily
export var TemporarySupportedModuleType;
(function (TemporarySupportedModuleType) {
    //Old spelling of DCA modules, currently using DATA_CAPTURE_SIGNUP
    TemporarySupportedModuleType["DATA_CAPTURE_SIGN_UP"] = "DATA_CAPTURE_SIGN_UP";
    //These modules should be massaged in frontend before sending to us
    TemporarySupportedModuleType["RYE_SHOPIFY_COLLECTION"] = "RYE_SHOPIFY_COLLECTION";
    TemporarySupportedModuleType["RYE_SHOPIFY_PRODUCT"] = "RYE_SHOPIFY_PRODUCT";
})(TemporarySupportedModuleType || (TemporarySupportedModuleType = {}));
//FIXME: remove this after data migration is done
//Older modules that is no longer supported / in use
export var RetiredModuleType;
(function (RetiredModuleType) {
    RetiredModuleType["RYE_SHOPIFY"] = "RYE_SHOPIFY";
    RetiredModuleType["SHOPIFY"] = "SHOPIFY";
    RetiredModuleType["ON_DEMAND_VIDEO"] = "ON_DEMAND_VIDEO";
    RetiredModuleType["EXPERIENCE"] = "EXPERIENCE";
    RetiredModuleType["FAN_CLUB"] = "FAN_CLUB";
    RetiredModuleType["YOUTUBE"] = "YOUTUBE";
    RetiredModuleType["MULTIPLE_YOUTUBE_VIDEO"] = "MULTIPLE_YOUTUBE_VIDEO";
})(RetiredModuleType || (RetiredModuleType = {}));
export var ModuleType;
(function (ModuleType) {
    ModuleType["LINK"] = "LINK";
    ModuleType["EXPERIENCE"] = "EXPERIENCE";
    ModuleType["ON_DEMAND_VIDEO"] = "ON_DEMAND_VIDEO";
    ModuleType["PRODUCT"] = "PRODUCT";
    ModuleType["MUSIC"] = "MUSIC";
    ModuleType["YOUTUBE_VIDEO"] = "YOUTUBE_VIDEO";
    ModuleType["PODCAST"] = "PODCAST";
    ModuleType["FAN_CLUB"] = "FAN_CLUB";
    ModuleType["SHOPIFY"] = "SHOPIFY";
    ModuleType["SHOPIFY_PRODUCT"] = "SHOPIFY_PRODUCT";
    ModuleType["SHOPIFY_COLLECTION"] = "SHOPIFY_COLLECTION";
    ModuleType["RYE_SHOPIFY"] = "RYE_SHOPIFY";
    ModuleType["RYE_SHOPIFY_PRODUCT"] = "RYE_SHOPIFY_PRODUCT";
    ModuleType["RYE_SHOPIFY_COLLECTION"] = "RYE_SHOPIFY_COLLECTION";
    ModuleType["BANDSINTOWN"] = "BANDSINTOWN";
    ModuleType["SHOP_MY_SHELF"] = "SHOP_MY_SHELF";
    ModuleType["SHOP_LIST"] = "SHOP_LIST";
    ModuleType["EVENTS"] = "EVENTS";
    ModuleType["FORM_DATA"] = "FORM_DATA";
    ModuleType["GROUP"] = "GROUP";
    ModuleType["YOUTUBE_COLLECTION"] = "YOUTUBE_COLLECTION";
    ModuleType["YOUTUBE"] = "YOUTUBE";
    ModuleType["PODCAST_AUTOMATION"] = "PODCAST_AUTOMATION";
    ModuleType["PODCAST_SELECT"] = "PODCAST_SELECT";
    ModuleType["SEATED"] = "SEATED";
    ModuleType["DATA_CAPTURE_SIGNUP"] = "DATA_CAPTURE_SIGNUP";
    ModuleType["DATA_CAPTURE_SECRET_LINK"] = "DATA_CAPTURE_SECRET_LINK";
    ModuleType["DATA_CAPTURE_COUPON_CODE"] = "DATA_CAPTURE_COUPON_CODE";
    ModuleType["TIKTOK_VIDEO"] = "TIKTOK_VIDEO";
    ModuleType["YOUTUBE_SHORT"] = "YOUTUBE_SHORT";
    ModuleType["MEDIA_GALLERY"] = "MEDIA_GALLERY";
    ModuleType["INSTAGRAM_REEL"] = "INSTAGRAM_REEL";
    ModuleType["TEXT"] = "TEXT";
    ModuleType["MAIL_LINK"] = "MAIL_LINK";
    ModuleType["THRONE_LINK"] = "THRONE_LINK";
    ModuleType["PAYPAL_LINK"] = "PAYPAL_LINK";
    ModuleType["VENMO_LINK"] = "VENMO_LINK";
    ModuleType["CASHAPP_LINK"] = "CASHAPP_LINK";
    ModuleType["PATREON_LINK"] = "PATREON_LINK";
    ModuleType["SPECIAL_OFFER_LINK"] = "SPECIAL_OFFER_LINK";
    ModuleType["CONTACT_FORM"] = "CONTACT_FORM";
    ModuleType["AFFILIATE"] = "AFFILIATE";
    ModuleType["DIGITAL_PRODUCT"] = "DIGITAL_PRODUCT";
    ModuleType["FREE_DOWNLOAD"] = "FREE_DOWNLOAD";
    ModuleType["PAID_DOWNLOAD"] = "PAID_DOWNLOAD";
    ModuleType["INSTAGRAM_AUTOMATION"] = "INSTAGRAM_AUTOMATION";
    ModuleType["TIKTOK_AUTOMATION"] = "TIKTOK_AUTOMATION";
})(ModuleType || (ModuleType = {}));
export const ZModule = z
    .object({
    //Legacy uses "groupId", new service uses "parentId"
    groupId: z.string().uuid().default(DEFAULT_UUID).optional().nullable(),
    id: z.string().uuid().optional(),
    items: z.array(z.any()),
    localizationId: z
        .string()
        .uuid()
        .nullable()
        .default(DEFAULT_UUID)
        .optional(),
    //Legacy uses "name", new service uses "title"
    name: z.string().optional().nullable(),
    order: z.number(),
    parentId: z.string().uuid().default(DEFAULT_UUID).optional().nullable(),
    profileId: z.string().uuid().optional(),
    showTitle: z.boolean().default(true),
    // Was this module created from a template and has not been edited?
    // Will be set to false once the user changes the template content
    template: z.boolean().default(false),
    title: z.string().optional().nullable(),
    //FIXME: remove this after data migration is done
    // type: z.nativeEnum(ModuleType),
    type: z
        .nativeEnum(ModuleType)
        .or(z.nativeEnum(TemporarySupportedModuleType))
        .or(z.nativeEnum(RetiredModuleType)),
    versionId: z.string().uuid().optional(),
    visible: z.boolean().default(true)
})
    //If value assigned for groupId / name (from legacy)
    //Convert them into new keys
    .transform(({ id, groupId, items, type, name, ...rest }) => ({
    groupId: groupId || null,
    //For modules with no uuid, create one
    id: id || uuidv4(),
    //FIXME: Convert Rye shopify modules into normal shopify modules
    //Remove these code after massaging has been integrated in other services
    //If it is RYE_* module, assign provider = 'rye'
    items: type.includes('RYE_')
        ? items.map(item => ({ ...item, provider: ShopifyProvider.RYE }))
        : //If it is not RYE_* but it is a shopify module, assign provider = 'komi'
            type.includes('SHOPIFY')
                ? items.map(item => ({ ...item, provider: ShopifyProvider.KOMI }))
                : //Else do nothing
                    items,
    //
    title: name,
    //If it is RYE_* module, remove the rye prefix
    type: (type.includes('RYE_')
        ? type.replace('RYE_', '')
        : //If it uses the old name (DATA_CAPTURE_SIGN_UP), format it
            type === TemporarySupportedModuleType.DATA_CAPTURE_SIGN_UP
                ? ModuleType.DATA_CAPTURE_SIGNUP
                : type),
    ...rest
}));
export const ZModulesBody = z.object({
    modules: z.array(ZModule)
});
export const ShopifyStoreTypes = [
    ModuleType.SHOPIFY,
    ModuleType.SHOPIFY_COLLECTION,
    ModuleType.SHOPIFY_PRODUCT
];
export const RyeShopifyStoreTypes = [
    ModuleType.RYE_SHOPIFY,
    ModuleType.RYE_SHOPIFY_COLLECTION,
    ModuleType.RYE_SHOPIFY_PRODUCT
];
