<!--
     This file is part of the happyDomain (R) project.
     Copyright (c) 2022-2024 happyDomain
     Authors: Pierre-Olivier Mercier, et al.

     This program is offered under a commercial and under the AGPL license.
     For commercial licensing, contact us at <contact@happydomain.org>.

     For AGPL licensing:
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU Affero General Public License as published by
     the Free Software Foundation, either version 3 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
-->

<script lang="ts">
    import {
        Button,
        Icon,
        Input,
        InputGroup,
        ListGroup,
        ListGroupItem,
        Spinner,
    } from "@sveltestrap/sveltestrap";

    import { addDomain } from "$lib/api/domains";
    import PickProvider, { controls as pickProviderControls } from "$lib/components/modals/PickProvider.svelte";
    import { fqdn, validateDomain } from "$lib/dns";
    import { navigate } from "$lib/stores/config";
    import { domains, domains_by_name, refreshDomains } from "$lib/stores/domains";
    import { filteredName, filteredProvider } from "$lib/stores/home";
    import type { Provider } from "$lib/model/provider";
    import { providers } from "$lib/stores/providers";
    import { t } from "$lib/translations";

    interface Props {
        autofocus?: boolean;
        noButton?: boolean;
        [key: string]: any;
    }

    let { autofocus = false, noButton = false, ...rest }: Props = $props();

    let addingNewDomain = $state(false);
    async function submitFilterDomainInput(e: SubmitEvent) {
        e.preventDefault();

        if (actionAddDomain) {
            addDomainToProvider();
        } else if (filteredDomains.length > 0) {
            navigate("/domains/" + encodeURIComponent(filteredDomains[0].id));
        }
    }

    function addDomainToProvider() {
        addingNewDomain = true;

        if ($filteredProvider) {
            addDomain($filteredName, $filteredProvider).then(
                (domain) => {
                    addingNewDomain = false;
                    filteredName.set("");
                    refreshDomains();
                },
                (error) => {
                    addingNewDomain = false;
                    throw error;
                },
            );
        } else if ($providers && $providers.length == 1) {
            addDomain($filteredName, $providers[0]).then(
                (domain) => {
                    addingNewDomain = false;
                    filteredName.set("");
                    refreshDomains();
                },
                (error) => {
                    addingNewDomain = false;
                    throw error;
                },
            );
        } else {
            addingNewDomain = false;
            pickProviderControls.Open();
        }
    }

    function onProviderSelected(provider: Provider) {
        addingNewDomain = true;
        addDomain($filteredName, provider).then(
            () => {
                addingNewDomain = false;
                filteredName.set("");
                refreshDomains();
            },
            (error) => {
                addingNewDomain = false;
                throw error;
            },
        );
    }

    let newDomainState: boolean | undefined = $derived(validateNewDomain($filteredName));
    function validateNewDomain(val: string): boolean | undefined {
        return validateDomain(val, "", false);
    }

    let filteredDomains = $derived(
        !$domains
            ? []
            : $domains.filter(
                  (dn) =>
                      dn.domain == fqdn($filteredName, "") &&
                      (!$filteredProvider ||
                          !$domains_by_name[fqdn($filteredName, "")].reduce(
                              (acc, d) => acc || d.id_provider == $filteredProvider._id,
                              false,
                          )),
              ),
    );
    let actionAddDomain = $derived(
        ($domains && $filteredName && filteredDomains.length == 0) ||
            ($domains && $domains.length == 0),
    );
</script>

<PickProvider ondone={onProviderSelected} />

<form onsubmit={submitFilterDomainInput}>
    <ListGroup {...rest}>
        <ListGroupItem class="d-flex justify-content-between align-items-center p-0">
            <InputGroup>
                <label
                    for="newdomaininput"
                    class="ms-2 my-1 text-center text-muted"
                    style="font-size: 1.6rem"
                >
                    {#if actionAddDomain}
                        <Icon name="plus-lg" />
                    {:else}
                        <Icon name="search" />
                    {/if}
                </label>
                <Input
                    id="newdomaininput"
                    {autofocus}
                    autocomplete="off"
                    class="font-monospace"
                    disabled={addingNewDomain}
                    placeholder={$t("domains.placeholder-search")}
                    invalid={$filteredName.length
                        ? newDomainState !== undefined && !newDomainState
                        : undefined}
                    valid={$filteredName.length ? newDomainState : undefined}
                    style="border:none;box-shadow:none;z-index:0"
                    bind:value={$filteredName}
                />
                {#if !noButton && $filteredName.length}
                    <Button
                        type="submit"
                        outline
                        color={actionAddDomain ? "primary" : "secondary"}
                        disabled={addingNewDomain}
                    >
                        {#if addingNewDomain}
                            <Spinner size="sm" class="me-1" />
                        {/if}
                        {#if actionAddDomain}
                            {$t("common.add-new-thing", { thing: $t("domains.kind") })}
                        {:else}
                            {$t("domains.view.title")}
                        {/if}
                    </Button>
                {/if}
            </InputGroup>
        </ListGroupItem>
    </ListGroup>
</form>
