import _extends from "babel-runtime/helpers/extends";
import _classCallCheck from "babel-runtime/helpers/classCallCheck";
import _createClass from "babel-runtime/helpers/createClass";
import _possibleConstructorReturn from "babel-runtime/helpers/possibleConstructorReturn";
import _inherits from "babel-runtime/helpers/inherits";
import React from 'react';
import { find } from "lodash";
import "antd/es/tag/style";
import "./index.less";
var ShowType;
(function (ShowType) {
    ShowType[ShowType["IMG"] = 1] = "IMG";
    ShowType[ShowType["NAME"] = 2] = "NAME";
    ShowType[ShowType["IMG_NAME"] = 3] = "IMG_NAME";
})(ShowType || (ShowType = {}));

var SkuParamSelector = function (_React$Component) {
    _inherits(SkuParamSelector, _React$Component);

    function SkuParamSelector(props) {
        _classCallCheck(this, SkuParamSelector);

        var _this = _possibleConstructorReturn(this, (SkuParamSelector.__proto__ || Object.getPrototypeOf(SkuParamSelector)).call(this, props));

        _this.renderAttrName = function (attrValue) {
            var showType = attrValue.showType,
                imgUrl = attrValue.imgUrl,
                value = attrValue.value;

            switch (showType) {
                case ShowType.IMG:
                    return React.createElement(
                        "a",
                        { href: "#", className: "itemA itemImg", style: { background: "url(" + imgUrl + ") center no-repeat" } },
                        React.createElement(
                            "span",
                            { title: value },
                            value
                        )
                    );
                case ShowType.IMG_NAME:
                    return React.createElement(
                        "a",
                        { href: "#", className: "itemA itemImgName", style: { background: "url(" + imgUrl + ") center no-repeat" } },
                        React.createElement(
                            "span",
                            { title: value },
                            value
                        )
                    );
                case ShowType.NAME:
                    return React.createElement(
                        "a",
                        { href: "#", className: "itemA itemName" },
                        React.createElement(
                            "span",
                            { title: value },
                            value
                        )
                    );
            }
            return '';
        };
        _this.onSelectClick = function (attr, attrValue) {
            var attrSelectedMap = _this.state.attrSelectedMap;
            var onAfterSelect = _this.props.onAfterSelect;

            attrSelectedMap[attr.attrId] = attrValue.value;
            var newAttrSelectedMap = _extends({}, attrSelectedMap, { attrId: attr.attrId }, { selectable: attrValue.selectable });
            // newAttrSelectedMap[attr.attrId] = attrValue.value;
            // this.setState({attrSelectedMap: newAttrSelectedMap});
            onAfterSelect && onAfterSelect(newAttrSelectedMap);
        };
        var attrSelectedMap = {};
        var datas = _this.props.datas;

        if (datas && datas.length) {
            datas.forEach(function (p) {
                var f = find(p.attrValues, function (p1) {
                    return p1.checked;
                });
                if (f) {
                    attrSelectedMap[p.attrId] = f.value;
                } else {
                    attrSelectedMap[p.attrId] = p.attrValues[0].value;
                }
            });
        }
        _this.state = {
            attrSelectedMap: attrSelectedMap
        };
        return _this;
    }

    _createClass(SkuParamSelector, [{
        key: "componentWillReceiveProps",
        value: function componentWillReceiveProps() {
            this.getCheckedData();
        }
    }, {
        key: "getCheckedData",
        value: function getCheckedData() {
            var attrSelectedMap = {};
            var datas = this.props.datas;

            if (datas && datas.length) {
                datas.forEach(function (p) {
                    var f = find(p.attrValues, function (p1) {
                        return p1.checked;
                    });
                    if (f) {
                        attrSelectedMap[p.attrId] = f.value;
                    } else {
                        attrSelectedMap[p.attrId] = p.attrValues[0].value;
                    }
                });
            }
            this.setState({
                attrSelectedMap: attrSelectedMap
            });
        }
    }, {
        key: "render",
        value: function render() {
            var _this2 = this;

            var datas = this.props.datas;
            var attrSelectedMap = this.state.attrSelectedMap;

            return React.createElement(
                "div",
                { className: "sku-param-selector" },
                datas && datas.length > 0 && datas.map(function (p) {
                    return React.createElement(
                        "div",
                        { className: "attr-item", key: p.attrId },
                        React.createElement(
                            "div",
                            { className: "itemTitle" },
                            p.attrName
                        ),
                        React.createElement(
                            "div",
                            null,
                            React.createElement(
                                "ul",
                                null,
                                p.attrValues && p.attrValues.length > 0 && p.attrValues.map(function (p2) {
                                    return React.createElement(
                                        "li",
                                        { key: p.attrId + "_" + p2.value, className: (attrSelectedMap[p.attrId] === p2.value ? 'selected' : '') + " " + (attrSelectedMap[p.attrId] !== p2.value && !p2.selectable ? 'unselectable' : ''), onClick: function onClick() {
                                                _this2.onSelectClick(p, p2);
                                            } },
                                        _this2.renderAttrName(p2)
                                    );
                                })
                            )
                        )
                    );
                })
            );
        }
    }]);

    return SkuParamSelector;
}(React.Component);

export default SkuParamSelector;

SkuParamSelector.defaultProps = {
    datas: []
};