import { Input, Label } 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 RecipientName() {
  const [orderDetails, setOrderDetails] = useOrderDetails();
  const orderRecipient = orderDetails?.order_recipient;

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

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

  return (
    <Container>
      <Wrapper>
        <Label label="Recipient Name" htmlFor="recipient-name" />
        {showCopyIcon && <CopyText text={orderRecipient?.recipient_name!} />}
      </Wrapper>
      <InputStyled
        name="recipient-name"
        type="text"
        placeholder="Recipient Name"
        value={orderRecipient?.recipient_name || ''}
        onChange={onChange}
        autoComplete="off"
      />
    </Container>
  );
}

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

const Container = styled.div`
  display: flex;
  flex-direction: column;
  gap: 6px;
`;
const InputStyled = styled(Input)`
  /* Hide autofill suggestions in Safari */
  border-radius: 25px;
  &:-webkit-autofill {
    -webkit-box-shadow: 0 0 0 100px white inset;
    -webkit-text-fill-color: black;
  }

  &:-webkit-autofill::first-line {
    font-family: 'inherit';
    font-size: inherit;
  }
`;
export default RecipientName;
