
import * as I from "../types/types";
import { config } from "./config";

export function processSMAP(): void {
    const inputsWithValue = config.state.inputsWithValue!;

    if (!inputsWithValue.smap)
        return;

    const smapArr: number[] = inputsWithValue.smap.split("").map(Number);

    if (!smapArr[0])
        jQuery("#eventInfoCapacity").hide();
}

export function getSectionDescBySectionID(inSectionID: string): string | undefined {
    const venueXML: I.VenueXML = config.state.inVenueXML!;
    const sectionArr: I.Section[] = venueXML.master_config[0].section_config[0].section;

    return sectionArr.find(arr => inSectionID === arr.id[0])?.desc[0];
}

export function getVenuePricescalePropertyByPricescaleID(inProperty: I.Pricescale2Properties, inPricescaleID: string): string | undefined {
    const venueXML: I.VenueXML = config.state.inVenueXML!;
    const venuePricescaleArr: I.Pricescale2[] = venueXML.venue[0].pricescales[0].pricescale;

    return venuePricescaleArr.find(obj => obj.id[0] === inPricescaleID ? obj : undefined)?.[inProperty][0];
}

export function getBuyerTypeCodeByBuyerTypeID(inBuyerTypeID: string): string | undefined {
    const venueXML: I.VenueXML = config.state.inVenueXML!;
    const venueBuyerTypeArr: I.BuyerType2[] = venueXML.venue[0].buyer_types[0].buyer_type;

    return venueBuyerTypeArr.find(arr => inBuyerTypeID === arr.id[0])?.code[0];
}

export function getPriceByBuyerTypeID(inBuyertypeID: string, inPricescaleObj: I.Pricescale5): number | undefined {
    const price = inPricescaleObj?.buyer_type.find(arr => arr.id[0] === inBuyertypeID)?.price[0];
    return price ? parseFloat(price) : undefined;
}

export function getVenuePriceStructurePropertyByPricescaleID(inID: string): I.Pricescale5 | undefined {
    const venueXML: I.VenueXML = config.state.inVenueXML!;
    const venuePricescaleArr: I.Pricescale5[] = venueXML.price_structure[0].pricescale;
    return venuePricescaleArr.find(obj => obj.id[0] === inID);
}

export function getBuyerTypesByPricescaleID(inPricescaleID: string): I.TypeBuyerTypeArr[] | undefined {
    const buyerTypesArr: I.BuyerType3[] | undefined = findBuyerTypesArrByPricescaleID(inPricescaleID);
    return buyerTypesArr ? buyerTypesArr.map(arr => [arr.id[0], arr.price[0], getBuyerTypeDesc(arr)]) : undefined;
}

export function generateSelectedSeatIndexes(): string {
    return (config.state.selectedSeatsArr.map(arr => arr.join(","))).join("|");
}

function findBuyerTypesArrByPricescaleID(inPricescaleID: string): I.BuyerType3[] | undefined {
    const venueXML: I.VenueXML = config.state.inVenueXML!;
    const venuePricescaleArr: I.Pricescale5[] = venueXML.price_structure[0].pricescale;

    return venuePricescaleArr.find(obj => obj.id[0] === inPricescaleID)?.buyer_type;
}

function getBuyerTypeDesc(inArr: I.BuyerType3): string | undefined {
    const venueXML: I.VenueXML = config.state.inVenueXML!;
    const buyerTypeArr: I.BuyerType2[] = venueXML.venue[0].buyer_types[0].buyer_type;

    return buyerTypeArr.find(obj => obj.id[0] === inArr.id[0] ? obj.desc[0] : undefined)?.desc[0];
}