import { Label, TextArea } from '@bjsdistribution/dms-component-library';
import React from 'react';
import styled from 'styled-components';
import CopyText from '../../../../../../common/copy/CopyText';
import { useOrderDetails } from '../../../../store';

function Address() {
  // show copy icon if there is input value
  const [orderDetails, setOrderDetails] = useOrderDetails();
  const orderRecipient = orderDetails?.order_recipient;

  const showCopyIcon =
    orderRecipient?.address !== undefined &&
    orderRecipient?.address !== null &&
    orderRecipient?.address?.length > 0;

  const onChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
    const val = e.target.value;
    const updatedOrderRecipient = {
      ...(orderRecipient === null
        ? { address: val }
        : { ...orderRecipient, address: val }),
    };
    setOrderDetails(prev => {
      return {
        ...prev,
        order_recipient: updatedOrderRecipient,
      };
    });
  };

  return (
    <Container>
      <Wrapper>
        <Label label="Address" htmlFor="address" />
        {showCopyIcon && <CopyText text={orderRecipient?.address!} />}
      </Wrapper>
      <TextContainer
        name="address"
        placeholder="Address"
        value={orderRecipient?.address || ''}
        onChange={onChange}
      />
    </Container>
  );
}

const Wrapper = styled.div`
  display: flex;
  align-items: center;
  gap: 5px;
`;

const Container = styled.div`
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
`;
const TextContainer = styled(TextArea)`
  resize: none;
  height: 70%;
  border-radius: 15px;
`;

export default Address;
