import React, { type PropsWithChildren } from 'react';
import styled from 'styled-components';

import type { CodeWalkthroughAttr } from '@redocly/config';

import {
  CodeWalkthroughStepsContext,
  CodeWalkthroughControlsStateContext,
} from '@redocly/theme/core/contexts';
import { CodePanel } from '@redocly/theme/markdoc/components/CodeWalkthrough/CodePanel';
import { useCodeWalkthrough } from '@redocly/theme/core/hooks';
import {
  CodeFilters,
  GetFilterState,
} from '@redocly/theme/markdoc/components/CodeWalkthrough/CodeFilters';

export type CodeWalkthroughProps = PropsWithChildren<CodeWalkthroughAttr>;

export function CodeWalkthrough({ children, steps, preview, ...attributes }: CodeWalkthroughProps) {
  const { controlsState, stepsState, files, downloadAssociatedFiles } = useCodeWalkthrough(
    steps,
    attributes,
  );
  const { activeFilters, getControlState, changeControlState } = controlsState;
  const { filtersElementRef } = stepsState;

  return (
    <CodeWalkthroughStepsContext.Provider value={stepsState}>
      <CodeWalkthroughControlsStateContext.Provider value={controlsState}>
        <CodeWalkthroughWrapper
          className="code-walkthrough"
          data-component-name="Markdoc/CodeWalkthrough/CodeWalkthrough"
        >
          <DocsPanel>
            <CodeFilters
              filters={activeFilters}
              getFilterState={getControlState as GetFilterState}
              handleFilterSelect={changeControlState}
              filtersElementRef={filtersElementRef}
            />
            <ContentWrapper>{children}</ContentWrapper>
          </DocsPanel>

          <CodePanel
            files={files}
            downloadAssociatedFiles={downloadAssociatedFiles}
            preview={preview}
          />
        </CodeWalkthroughWrapper>
      </CodeWalkthroughControlsStateContext.Provider>
    </CodeWalkthroughStepsContext.Provider>
  );
}

const CodeWalkthroughWrapper = styled.div`
  display: grid;
  grid-template-columns: 4fr 6fr;

  padding-right: var(--spacing-xl);

  border-top: 1px solid var(--border-color-secondary);
  border-bottom: 1px solid var(--border-color-secondary);

  &:first-child {
    border-top: none;
  }

  &:last-child {
    border-bottom: none;
  }
`;

const DocsPanel = styled.div`
  display: flex;
  flex-direction: column;

  min-height: 0;
  min-height: calc(100vh - var(--navbar-height));

  padding-right: var(--spacing-xs);
  padding-bottom: calc(var(--spacing-xs) + var(--spacing-xl));
  gap: var(--spacing-xl);
`;

const ContentWrapper = styled.div`
  flex-grow: 1;

  min-height: 0;
  max-width: var(--md-content-max-width);

  & > *:not(.code-step-wrapper) {
    padding-left: var(--spacing-xl);
  }
  overflow-y: scroll;

  /* Hide scrollbar for Chrome, Safari and Opera */
  &::-webkit-scrollbar {
    display: none;
  }
  /* Hide scrollbar for IE, Edge and Firefox */
  -ms-overflow-style: none; /* IE and Edge */
  scrollbar-width: none; /* Firefox */

  &:first-child {
    padding-top: var(--spacing-sm);
  }

  &:not(:first-child) {
    & > *:first-child {
      margin-top: 0;
    }
  }
`;
