import { FrontCoverInterface, ProductViewInterface, SinglePageViewInterface, } from './index'; import { LayoutBoxInterface, } from '../index'; export default class ProductViewModel { public static isFrontCover( productView: ProductViewInterface, ) { const frontCover = productView as FrontCoverInterface; return Boolean( frontCover && frontCover.title, ); } public static isSinglePageView( productView: ProductViewInterface, ) { const singleView = productView as SinglePageViewInterface; return Boolean( singleView && singleView.isSingleLeftPage !== undefined, ); } public static isFirstPageView( productView: ProductViewInterface, ) { const singleView = productView as SinglePageViewInterface; return Boolean( ProductViewModel.isSinglePageView(productView) && !singleView.isSingleLeftPage, ); } public static isLastPageView( productView: ProductViewInterface, ) { const singleView = productView as SinglePageViewInterface; return Boolean( ProductViewModel.isSinglePageView(productView) && singleView.isSingleLeftPage, ); } public static getTotalNumberOfPages( allProductViews: ProductViewInterface[], ) { const normalPages = allProductViews.filter((productView) => ( !ProductViewModel.isFrontCover(productView) && !ProductViewModel.isSinglePageView(productView) )).length * 2; return normalPages + allProductViews.filter( ProductViewModel.isSinglePageView, ).length; } public static findWithId( productViews: ProductViewInterface[], id: number, ) { return productViews.find((productView) => ( productView.id === id )); } public static findByContainedLayoutBox( productViews: ProductViewInterface[], layoutBox: LayoutBoxInterface, ) { return productViews.find((productView) => { return productView.layoutBoxes.some( (box) => (box.id === layoutBox.id), ); }); } }