import React, { useState, useEffect, useMemo } from 'react';
import { Form, Radio } from 'antd';
import { findValuesByNamePath } from '../../../../utils';

const xAlignOptions = [
  { label: '居左', value: 'left' },
  { label: '自动-左', value: 'auto-left' },
  { label: '自动-右', value: 'auto-right' },
  { label: '居右', value: 'right' },
];
export default ({ propValue, config, onChange = () => {} }) => {
  const { name } = config;
  const monitor = propValue;
  const [statePosition, setStatePosition] = useState();
  const [stateType, setStateType] = useState();

  const handleChange = (key, value) => {
    setStatePosition(value);
    onChange(key, value);
  };

  useEffect(() => {
    const { position, type } = findValuesByNamePath(name, monitor);
    setStateType(type);
    setStatePosition(position);
  }, [monitor]);

  const memo_Dom = useMemo(() => {
    return (
      <>
        {stateType && (
          <Form.Item label="图标位置">
            <div className="cell-row">
              <Radio.Group
                size="small"
                options={xAlignOptions}
                onChange={e => {
                  handleChange(name.position, e.target.value);
                }}
                value={statePosition}
                optionType="button"
              />
            </div>
          </Form.Item>
        )}
      </>
    );
  }, [statePosition, stateType]);

  return <>{memo_Dom}</>;
};
