/* @jsx createElement */

'use strict';

import { Component, createElement } from 'rax';
import SwitchComponent from './view/switch';

const noop = () => {};
class Switch extends Component {
  constructor(props) {
    super(props);
    let checked = false;
    if ('checked' in props) {
      checked = props.checked;
    } else {
      checked = props.defaultChecked;
    }

    this.state = {
      checked,
    };
    this.onValueChange = this.onValueChange.bind(this);
  }

  onValueChange(newValue) {
    this.setState({ checked: newValue });
    this.props.onValueChange && this.props.onValueChange(newValue);
  }

  render() {
    const { checked } = this.state;
    return (
      <SwitchComponent
        {...this.props}
        onValueChange={this.onValueChange}
        value={checked}
      />
    );
  }
}
Switch.defaultProps = {
  onValueChange: noop,
};
export default Switch;
