/* like mutation */
import ACTION from '../actions/actiontsTypes';
import {groups, brands, genders, countryFragrance} from "../constants/goods";


const initialState = {
    goodsData: {},
    // goodsAttributes: [],
    // newImages: [],
    isFetching: false,
    error: null
};

export default function (state = initialState, action) {
    switch (action.type) {
        case ACTION.ADMIN_GOODS_REQUEST: {
            return {
                ...state,
                isFetching: true,
                error: null
            }
        }
        case ACTION.ADMIN_GOODS_RESPONSE: {
            return {
                ...state,
                // data: action.data,
                // newImages: [],
                goodsData: action.data,
                // goodsAttributes: action.data.GoodsAttributes,
                isFetching: false,
                error: null
            }
        }
        case ACTION.ADMIN_CLEAR_GOODS_DATA: {
            return {
                ...state,
                // data: action.data,
                // newImages: [],
                goodsData: {},
                // goodsAttributes: action.data.GoodsAttributes,
                isFetching: false,
                error: null
            }
        }
        // case ACTION.ADMIN_UPDATE_GOODS_ATTRIBUTE: {
        //     const {goodsAttributesData, idAttribute, isNewAttribute} = action;
        //     const {imgUrlArray} = goodsAttributesData;
        //     const cloneGoodsAttributes = state.goodsAttributes.concat();
        //     const newImages = state.newImages.concat();
        //     const newImgUrlArray = [];
        //
        //
        //     cloneGoodsAttributes.forEach((elem, index, array) => {
        //         if(elem.id === idAttribute && !isNewAttribute) {
        //             if(!imgUrlArray) {
        //                 Object.assign(goodsAttributesData, {
        //                     id: idAttribute,
        //                     imgUrlArray: elem.imgUrlArray
        //                 });
        //             }else {
        //
        //                 elem.imgUrlArray.map(elem => {
        //                     newImgUrlArray.push(elem);
        //                 });
        //                 Array.from(imgUrlArray).forEach(img => {
        //                     newImgUrlArray.push(img.name);
        //                     newImages.push(img);
        //                 });
        //                 Object.assign(goodsAttributesData, {
        //                    id: idAttribute,
        //                    imgUrlArray: newImgUrlArray
        //                 });
        //             }
        //             array.splice(index, 1, goodsAttributesData);
        //         }
        //     });
        //     if(isNewAttribute) {
        //         Array.from(imgUrlArray).forEach(img => {
        //             newImgUrlArray.push(img.name);
        //             newImages.push(img);
        //         });
        //         Object.assign(goodsAttributesData, {
        //             imgUrlArray: newImgUrlArray
        //         });
        //         cloneGoodsAttributes.push(goodsAttributesData);
        //     }
        //
        //
        //   return {
        //       ...state,
        //       goodsAttributes: cloneGoodsAttributes,
        //       newImages: newImages,
        //       isFetching: false,
        //       error: null
        //   }
        // }
        case ACTION.ADMIN_GOODS_ERROR: {
            return {
                ...state,
                error: action.error,
                isFetching: false
            }
        }
        default: {
            return state
        }
    }
}

