"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const factory = require("../factory");
/**
 * 上映イベントに対する券種オファーを検索する
 */
function searchScreeningEventTicketOffers(params) {
    // tslint:disable-next-line:max-func-body-length
    return (repos) => __awaiter(this, void 0, void 0, function* () {
        const event = yield repos.event.findById({
            typeOf: factory.eventType.ScreeningEvent,
            id: params.eventId
        });
        const superEvent = yield repos.event.findById(event.superEvent);
        const eventSoundFormatTypes = (Array.isArray(event.superEvent.soundFormat)) ? event.superEvent.soundFormat.map((f) => f.typeOf) : [];
        const eventVideoFormatTypes = (Array.isArray(event.superEvent.videoFormat))
            ? event.superEvent.videoFormat.map((f) => f.typeOf)
            : [factory.videoFormatType['2D']];
        const ticketTypes = yield repos.ticketType.findByTicketGroupId({ ticketGroupId: event.ticketTypeGroup });
        // 価格仕様を検索する
        const soundFormatCompoundPriceSpecifications = yield repos.priceSpecification.searchCompoundPriceSpecifications({
            typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
            priceComponent: { typeOf: factory.priceSpecificationType.SoundFormatChargeSpecification }
        });
        const videoFormatCompoundPriceSpecifications = yield repos.priceSpecification.searchCompoundPriceSpecifications({
            typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
            priceComponent: { typeOf: factory.priceSpecificationType.VideoFormatChargeSpecification }
        });
        const movieTicketTypeCompoundPriceSpecifications = yield repos.priceSpecification.searchCompoundPriceSpecifications({
            typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
            priceComponent: { typeOf: factory.priceSpecificationType.MovieTicketTypeChargeSpecification }
        });
        // イベントに関係のある価格仕様に絞り、ひとつの複合価格仕様としてまとめる
        const soundFormatChargeSpecifications = soundFormatCompoundPriceSpecifications.reduce((a, b) => [...a, ...b.priceComponent], []).filter((spec) => eventSoundFormatTypes.indexOf(spec.appliesToSoundFormat) >= 0);
        const videoFormatChargeSpecifications = videoFormatCompoundPriceSpecifications.reduce((a, b) => [...a, ...b.priceComponent], []).filter((spec) => eventVideoFormatTypes.indexOf(spec.appliesToVideoFormat) >= 0);
        const movieTicketTypeChargeSpecs = movieTicketTypeCompoundPriceSpecifications.reduce((a, b) => [...a, ...b.priceComponent], []).filter((spec) => eventVideoFormatTypes.indexOf(spec.appliesToVideoFormat) >= 0);
        const eventOffers = Object.assign({}, superEvent.offers, event.offers);
        // ムビチケが決済方法として許可されていれば、ムビチケオファーを作成
        let movieTicketOffers = [];
        const movieTicketPaymentAccepted = eventOffers.acceptedPaymentMethod === undefined
            || eventOffers.acceptedPaymentMethod.indexOf(factory.paymentMethodType.MovieTicket) >= 0;
        if (movieTicketPaymentAccepted) {
            movieTicketOffers = ticketTypes
                .filter((ticketType) => ticketType.eligibleMovieTicketType !== undefined && ticketType.eligibleMovieTicketType !== '')
                .map((ticketType) => {
                const movieTicketType = ticketType.eligibleMovieTicketType;
                const unitPriceSpecification = {
                    typeOf: factory.priceSpecificationType.UnitPriceSpecification,
                    price: ticketType.price,
                    priceCurrency: factory.priceCurrency.JPY,
                    name: ticketType.name,
                    description: ticketType.description,
                    valueAddedTaxIncluded: true,
                    referenceQuantity: ticketType.eligibleQuantity
                };
                const mvtkSpecs = movieTicketTypeChargeSpecs.filter((s) => s.appliesToMovieTicketType === movieTicketType);
                const priceComponent = [
                    unitPriceSpecification,
                    ...mvtkSpecs
                ];
                const compoundPriceSpecification = {
                    typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
                    priceCurrency: factory.priceCurrency.JPY,
                    valueAddedTaxIncluded: true,
                    priceComponent: priceComponent
                };
                return {
                    typeOf: 'Offer',
                    id: ticketType.id,
                    name: ticketType.name,
                    description: ticketType.description,
                    valueAddedTaxIncluded: true,
                    priceCurrency: factory.priceCurrency.JPY,
                    priceSpecification: compoundPriceSpecification,
                    availability: factory.itemAvailability.InStock,
                    availabilityEnds: eventOffers.availabilityEnds,
                    availabilityStarts: eventOffers.availabilityStarts,
                    eligibleQuantity: eventOffers.eligibleQuantity,
                    validFrom: eventOffers.validFrom,
                    validThrough: eventOffers.validThrough
                };
            });
        }
        // ムビチケ以外のオファーを作成
        const ticketTypeOffers = ticketTypes
            .filter((ticketType) => ticketType.eligibleMovieTicketType === undefined || ticketType.eligibleMovieTicketType === '')
            .map((ticketType) => {
            const unitPriceSpecification = {
                typeOf: factory.priceSpecificationType.UnitPriceSpecification,
                price: ticketType.price,
                priceCurrency: factory.priceCurrency.JPY,
                name: ticketType.name,
                description: ticketType.description,
                valueAddedTaxIncluded: true,
                referenceQuantity: ticketType.eligibleQuantity
            };
            const priceComponent = [
                unitPriceSpecification,
                ...videoFormatChargeSpecifications,
                ...soundFormatChargeSpecifications
            ];
            const compoundPriceSpecification = {
                typeOf: factory.priceSpecificationType.CompoundPriceSpecification,
                priceCurrency: factory.priceCurrency.JPY,
                valueAddedTaxIncluded: true,
                priceComponent: priceComponent
            };
            return {
                typeOf: 'Offer',
                id: ticketType.id,
                name: ticketType.name,
                description: ticketType.description,
                priceCurrency: factory.priceCurrency.JPY,
                priceSpecification: compoundPriceSpecification,
                availability: ticketType.availability,
                availabilityEnds: eventOffers.availabilityEnds,
                availabilityStarts: eventOffers.availabilityStarts,
                eligibleQuantity: eventOffers.eligibleQuantity,
                validFrom: eventOffers.validFrom,
                validThrough: eventOffers.validThrough
            };
        });
        return [...ticketTypeOffers, ...movieTicketOffers];
    });
}
exports.searchScreeningEventTicketOffers = searchScreeningEventTicketOffers;